UVa 548 Tree
来源:互联网 发布:淘宝刷心悦会封号吗 编辑:程序博客网 时间:2024/06/03 23:44
#include <cctype>#include <climits>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <bitset>#include <deque>#include <fstream>#include <iostream>#include <list>#include <map>#include <queue>#include <set>#include <sstream>#include <stack>#include <string>#include <utility>#include <vector>using namespace std;#define VI vector<int>#define ALL(x) (x).begin(),(x).end()#define SZ(x) (x).size()//int res;struct Node{ int val; Node *lchild; Node *rchild;};Node* buildtree(const VI& x,const VI& y){ Node* crt = NULL; if(!x.empty()){ int cnt = y.back(); crt = new Node; crt->lchild = crt->rchild = NULL; VI::const_iterator it = find(ALL(x),cnt); VI p1(x.begin(),it); VI p2(++it,x.end()); VI q1(y.begin(),y.begin()+SZ(p1)); VI q2(y.begin()+SZ(p1),y.begin()+SZ(p1)+SZ(p2)); crt->val = cnt; crt->lchild = buildtree(p1,q1); crt->rchild = buildtree(p2,q2); } return crt;}void dfs(Node *node, int sum,int& res, int& ret){ //闹了半天,原来是这里的 sum 不能用 int& if(node){ sum += node->val; if(!node->lchild&&!node->rchild&&res>sum){ res = sum; ret = node->val; } else{ dfs(node->lchild,sum,res,ret); dfs(node->rchild,sum,res,ret); } }}int main(){ //freopen("in.txt","r",stdin); string s; while(getline(cin, s)){ int x; vector<int> A; vector<int> B; stringstream s1(s); while(s1>>x)A.push_back(x); getline(cin, s); stringstream s2(s); while(s2>>x)B.push_back(x); Node *tree = buildtree(A,B); int ret = 0,sum = 0; int res = INT_MAX; dfs(tree, sum, res, ret); printf("%d\n",ret); } 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
- 关于struts2的配置
- 浅谈土地利用图件缩编方法的应用研究
- UVa 536 Tree Recovery
- UVa 10895 Matrix Transpose
- 使用HAProxy、PHP、Redis和MySQL支撑10亿请求每周架构细节
- UVa 548 Tree
- UVa 11995 I Can Guess the Data Structure!
- Android笔记之——framework框架
- 求两个日期之间间隔的天数,Python实现
- Android笔记之——APK的运行状态
- Android笔记——-Binder类框架
- PAT 1044. Shopping in Mars
- PAT_B_字符串-04. 字符串逆序(15)
- PAT_B_字符串-02. 删除字符串中的子串(20)