【例题 6-8 UVA - 548】Tree
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
后序遍历的最后一个是根节点。
->然后在中序遍历中找到这个节点。
分为左右两段.
然后递归上述操作就好。
题目描述好坑啊。
原来是叶子节点的权值的最小值。。
(叶子节点到根节点的权值和最小的对应的叶子节点的最小权值,,,)
【代码】
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e4;
const ll INF = 1e18;
string s;
int zx[N+10],hx[N+10],n,g[N+10][2],val[N+10],cnt;
int idx[N+10],cur,ansi = N+10;
ll ans = INF;
void init(int a[]){
stringstream ss(s);
n = 1;
while (ss>>a[n]){
n++;
}
n--;
}
void dfs1(int x,int l,int r){
val[x] = hx[cur];
int mid = idx[hx[cur]];
cur--;
if (mid<r) {
if (!g[x][1]) g[x][1] = ++cnt;
dfs1(g[x][1],mid+1,r);
}
if (l < mid){
if (!g[x][0]) g[x][0] = ++cnt;
dfs1(g[x][0],l,mid-1);
}
}
void dfs(int x,ll sum){
if (!g[x][0] && !g[x][1]){
if (sum < ans){
ans = sum;
ansi = val[x];
}else if (ans==sum)
ansi = min(ansi,val[x]);
return;
}
if (g[x][0]) dfs(g[x][0],sum+val[g[x][0]]);
if (g[x][1]) dfs(g[x][1],sum+val[g[x][1]]);
}
int main(){
// freopen("rush.txt","r",stdin);
while (getline(cin,s)){
ans = INF,ansi = N+2;
cnt = 1;
memset(g,0,sizeof g);
init(zx);
getline(cin,s);
init(hx);
for (int i = 1;i <= n;i++) idx[zx[i]] = i;
cur = n;
dfs1(1,1,n);
dfs(1,val[1]);
printf("%lld\n",ansi);
}
return 0;
}
【例题 6-8 UVA - 548】Tree的更多相关文章
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- UVa 548 Tree(二叉树最短路径)
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- UVa 548 Tree (建树+前序后序)
Description You are to determine the value of the leaf node in a given binary tree that is the termi ...
- Uva 548 Tree
0.这是一道利用中序遍历和后序遍历确定二叉树的题目,学会建树 关键点理解这段代码 int build(int L1,int R1,int L2,int R2) { //printf("bui ...
- UVa 548 Tree【二叉树的递归遍历】
题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困 ...
- UVA 548 Tree 建树
题意: 输入中序和后序的权值,输出哪个叶子使它到根的路径上权和最小. 思路: 输入后建树,然后dfs求最小的叶子. #include<iostream> #include<cstdi ...
- uva 548 Tree(通过后序,先序重建树+dfs)
难点就是重建树,指针參数的传递今天又看了看.应该是曾经没全然弄懂.昨天真没效率,还是不太专心啊.以后一定得慢慢看.不能急躁,保持寻常心,. 分析: 通过兴许序列和中序序列重建树,用到了结构体指针.以及 ...
- UVA - 548 Tree(二叉树的递归遍历)
题意:已知中序后序序列,求一个叶子到根路径上权和最小,如果多解,则叶子权值尽量小. 分析:已知中序后序建树,再dfs求从根到各叶子的权和比较大小 #include<cstdio> #inc ...
- UVa 548 Tree(中序遍历+后序遍历)
给一棵点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小.如果有多解,该叶子本身的权应尽量小.输入中每两行表示一棵树,其中第一行为中序遍 ...
- Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
随机推荐
- hadoop集群环境配置成功与否查看方法
1.Hadoop启动jobhistoryserver来实现web查看作业的历史运行情况,由于在启动hdfs和Yarn进程之后,jobhistoryserver进程并没有启动,需要手动启动jobhist ...
- Linux系统下到哪儿寻找硬件错误
Linux系统下到哪儿寻找硬件错误 当linux系统出现故障的时候,作为管理员首先要定位错误,现在linux有许多工具都能帮助用户寻找错误,要学会利用他们确定问题.这些工具包括dmesg. ...
- 【Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) C】 Travelling Salesman and Special Numbers
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 会发现. 进行一次操作过后. 得到的数字肯定是<=1000的 然后1000以下可以暴力做的. 则我们枚举第1步后得到的数字x是 ...
- 47.Android 自己定义PopupWindow技巧
47.Android 自己定义PopupWindow技巧 Android 自己定义PopupWindow技巧 前言 PopupWindow的宽高 PopupWindow定位在下左位置 PopupWin ...
- CSS控制显示超出部分,用省略号显示
经常使用.可是常忘,我又不是写css的.所以记下来: 先设置一下限制的宽度, display:block; white-space:nowrap; overflow:hidden; text-over ...
- poj2796
#include <cstdio> /* * source poj.2796 * 题目: * 给定一个非负数的数组 其中value[l,r] = sum(l,r) * min (l,r); ...
- jquery如何阻止子元素相应mouseout事件
jquery如何阻止子元素相应mouseout事件:mouseout有一个特点,当鼠标移入子元素的时候,也会触发此事件,但是在实际应用中这个特点往往不是我们想要的,下面就通过代码实例介绍一下如何实现此 ...
- Scala中的“=>”和“<-”
“=>”符号大概可以看做是创建函数实例的语法糖,例如 args.foreach(arg => println(arg)) 大概可以看做 args.foreach(Function(arg) ...
- Shiro学习总结(10)——Spring集成Shiro
1.引入Shiro的Maven依赖 [html] view plain copy <!-- Spring 整合Shiro需要的依赖 --> <dependency> <g ...
- HDU——T 3579 Hello Kiki
http://acm.hdu.edu.cn/showproblem.php?pid=3579 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...