树,UVA 548
来源:互联网 发布:蜂窝移动网络搜索 导航 编辑:程序博客网 时间:2024/06/06 05:38
#include<iostream>#include<string>#include<sstream>#include<algorithm>using namespace std;const int maxv = 1000 + 10;int in_order[maxv],post_order[maxv],lch[maxv],rch[maxv];int n;bool read_list(int *a){ string line; if(!getline(cin,line)) return false; stringstream ss(line); n = 0; int x; while(ss >> x) a[n++] = x; return n > 0;}///把in_order[L1...R1]和post_order[L2...R2]建成一棵二叉树,返回树根int build(int L1,int R1,int L2,int R2){ if(L1 > R1) return 0; int root = post_order[R2]; int p = L1; while(in_order[p] != root) p++; int cnt = p-L1; ///左子树结点个数 lch[root] = build(L1,p-1,L2,L2+cnt-1); rch[root] = build(p+1,R1,L2+cnt,R2-1); return root;}int best,best_sum; ///目前为止的最优解和对应的和void dfs(int u,int sum){ sum += u; if(!lch[u] && rch[u]){ if(sum < best_sum || (sum == best_sum && u < best)){ best = u;best_sum = sum; } } if(lch[u]) dfs(lch[u],sum); if(rch[u]) dfs(rch[u],sum);}int main(void){ while(read_list(in_order)){ read_list(post_order); build(0,n-1,0,n-1); best_sum = 1000000000; dfs(post_order[n-1],0); cout << best << "\n"; } return 0;}
阅读全文
0 0
- 重写 UVA 548 树
- uva 548(二叉树)
- UVa 548 树
- uva 548 tree 树
- 548UVa树
- UVA 548 树
- 树,UVA 548
- 二叉树 UVA 548 Tree
- UVA - 548 Tree 二叉树
- UVa--548 Tree(二叉树)
- UVA 548 Tree 【树+DFS】
- uva 548
- UVA 548
- uva 548
- uva 548
- uva 548
- uva 548
- uva 548
- 计算机视觉期刊会议论文相关
- 如何解决类似 /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found 的报错
- 对于oracle sql左连接的一点认识
- 引用数据类型
- Mysterious Bacteria ( lightOJ 1220)
- 树,UVA 548
- python3.6基础学习记录(一)
- Java集合
- Redis 集群端口
- 1076. Forwards on Weibo (30)
- iOS_常用的小功能
- Vijos / 题库 / SuperBrother打鼹鼠 1512
- mysql5.7.19用户的创建和权限的操作
- java做空心菱形