UVA_548Tree
这是一个很经典的建树,然而当时不会!!!!
给你一个中序和后序 先建一个二叉树,然后找最优解(最优解就是一个叶子节点到根节点权值最小, 同时本身权值最小)
//生成一棵树
int build(int L1, int R1, int L2, int R2) { //表示1为中序,2为后序 生成这个二叉树 其中R2为这棵树的根
if(L1 > R1) return 0;
int root = post_order[R2];
int p = L1;
while(in_order[p] != root) ++p; //在中序中找到根的位置
int cnt = p - L1; //cnt表示当前这棵树左子树的大小
lch[root] = build(L1, p-1, L2, L2 + cnt -1);//这里要知道在后序中,左子树(cnt),右子树,根
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);
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<sstream>
using namespace std;
const int maxn = 1e4+100;
int in_order[maxn], post_order[maxn], lch[maxn], rch[maxn];
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;
} 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(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
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;
}
UVA_548Tree的更多相关文章
随机推荐
- 从FreeBSD里面看到的网络协议列表,感觉可以保存一下
# # Internet protocols # # $FreeBSD$ # from: @(#)protocols 5.1 (Berkeley) 4/17/89 # # See also http: ...
- Droppable(放置组件)
一.class加载方式 <div id="pop" class="easyui-droppable" style="width: 400px;h ...
- javascript基础7(正则及表单验证)
1.正则的概念 JS诞生的目的是什么? 就是为了做表单验证. 在JS未出现以前,表单的信息验证需要传输给后台,让后台做数据验证处理之后,再返回给前端页面处理的结果.在带宽有 ...
- 【异常】Caused by: java.lang.IllegalStateException: Method has too many Body parameters
出现此异常原因是引文使用feign客户端的时候,参数没有用注解修饰 1.1GET方式错误写法 @RequestMapping(value="/test", method=Reque ...
- 分支结构case 语句语法
- 三、IIS通过目录方式部署以供外部调试
一.IIS 下面是通过 gif 为 因项目是bin生成后的,非运行方式的调试,所以断点调试无效,仅修改文件后,右击项目重新生成解决方案即可,好处:启动快,坏处:不可以断点调试查看变量和分步执行语句.
- PROXY——代理模式
代理,说白了就是中介.假设有俩对象A和B,A想访问B,但是根据迪米特法则,我们不能喝陌生人说话,简而言之就是A要减少知道B的相关情况,要降低A与B的耦合度.这时我们使用中介C,而C拥有B的相关情况,A ...
- go语言从例子开始之Example27.超时处理
超时 对于一个连接外部资源,或者其它一些需要花费执行时间的操作的程序而言是很重要的.得益于通道和 select,在 Go中实现超时操作是简洁而优雅的. Example: package main im ...
- java 三元运算符
/* 一元运算符:只需要一个数据就可以进行操作的运算符 如:取反! 自增++ 自减 -- 二元运算符:需要两个数据才可以进行操作的运算符 如:加法+ 赋值= 三元运算符: 需要三个数据才可以进行操作的 ...
- 07.interrupt
/** *isInterrupted */ public class InterruptDemo { public static void main(String[] args) throws Int ...