uva 548 Tree
来源:互联网 发布:linux cp覆盖文件 编辑:程序博客网 时间:2024/06/05 15:03
#include<iostream>#include<cstdio>#include<sstream>using namespace std;const int maxv=10000+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 true;}int buildtree(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 len=p-L1; lch[root]=buildtree(L1,p-1,L2,L2+len-1); rch[root]=buildtree(p+1,R1,L2+len,R2-1); return root;}int best_sum=10000000,best;void dfs(int u,int sum){ sum+=u; if(!lch[u]&&!rch[u]) { if(best_sum>sum||(best_sum==sum&&u<best)) {best=u;best_sum=sum;} } if(lch[u]) dfs(lch[u],sum); if(rch[u]) dfs(rch[u],sum);}int main(){ while(read_list(in_order)) { read_list(post_order); buildtree(0,n-1,0,n-1); int u=post_order[n-1]; best_sum=1000000; dfs(u,0); cout<<best<<endl; } return 0;}
0 0
- UVa 548 Tree
- uva 548 Tree
- uva 548 - Tree
- uva 548 - Tree
- UVa 548 - Tree
- uva 548 Tree
- uva 548 - Tree
- 数据结构 uva 548-Tree
- UVA 548 - Tree
- UVa 548 - Tree
- UVa 548 - Tree
- uva-548 Tree
- UVA 548 Tree
- UVA 548 tree
- uva 548 -Tree
- uva 548 Tree
- 548 - Tree (UVa OJ)
- UVA 548 Tree
- iis.bat、w3wp.bat
- SCU 4512 Goozy的积木(dp)
- list set map
- oracle锁表后,系统能否自动解锁?
- java入门教程-3.5Java方法重载
- uva 548 Tree
- 树莓派 屏幕显示 config.txt
- vs2010之opencv调试
- linux tail命令的使用方法详解
- SSH学习一
- java入门教程-3.6Java类的基本运行顺序
- iOS开发浮点数问题
- Markdown 11种基本语法
- java入门教程-3.7Java包装类、拆箱和装箱详解