【链接】 我是链接,点我呀:)

【题意】

在这里输入题意

【题解】

可以先确定当前这棵子树的dfs序的范围。
然后第一个元素肯定是这棵子树的根节点。
那么只要在这棵子树的范围里面枚举节点。
看看有没有下一个bfs序的节点即可。
如果有的话,那么就说明这个根节点有多个子树。
则加入到它的儿子里面去。
然后确定他的每一个儿子的子树的dfs序的范围。
一直往下递归就好。
用bfs模拟,这样可以保证每次进入某个节点的时候,bfs序的下一个节点就是这个子树的第一个儿子节点。

【代码】

/*
1.Shoud it use long long ?
2.Have you ever test several sample(at least therr) yourself?
3.Can you promise that the solution is right? At least,the main ideal
4.use the puts("") or putchar() or printf and such things?
5.init the used array or any value?
6.use error MAX_VALUE?
7.use scanf instead of cin/cout?
8.whatch out the detail input require
*/
#include <bits/stdc++.h>
using namespace std; const int N = 1e3; int n, B[N + 5], D[N + 5];
queue < pair<int, int> > dl;
vector <int> G[N + 10]; int main() {
#ifdef LOCAL_DEFINE
freopen("F:\\c++source\\rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0), cin.tie(0);
while (cin >> n) {
for (int i = 1; i <= n; i++) cin >> B[i];
for (int i = 1; i <= n; i++) cin >> D[i];
for (int i = 1; i <= n; i++) G[i].clear();
dl.push(make_pair(1, n));
int now = 2;
while (!dl.empty()) {
int l = dl.front().first, r = dl.front().second;
dl.pop();
int root = D[l];
if (l >= r) continue; G[root].push_back(B[now]);
now++;
if (now > n) continue; int pre = l + 1; for (int i = l + 2; i <= r; i++) {
if (now <= n && D[i] == B[now]) {
dl.push(make_pair(pre, i-1));
pre = i;
G[root].push_back(D[i]);
now++;
}
}
dl.push({ pre, r });
}
for (int i = 1; i <= n; i++) {
cout << i << ":";
for (int x : G[i]) cout << ' ' << x;
cout << endl;
}
}
return 0;
}

【习题 6-11 UVA - 10410】Tree Reconstruction的更多相关文章

  1. UVA 10410 Tree Reconstruction

    题意: 给定一个树的BFS和DFS,求这棵树. 分析: 拿dfs的序列,分成若干段,每一段相当一个子树,这样就可以利用bfs的序列去将dfs的序列分段,然后利用一个队列去存放每一段,不断求出子树即可. ...

  2. UVA - 10410 Tree Reconstruction (根据dfs序和bfs序恢复一颗树)

    题意: 分析: 这题一开始完全没有思路, 一直没有找出规律. 参考了http://www.cnblogs.com/Wade-/p/6358859.html 和 http://www.cnblogs.c ...

  3. UVA - 10410 Tree Reconstruction(栈处理递归)

    题目: 给出一棵树的BFS和DFS序列,输出这棵树中每个结点的子结点列表.BFS和DFS序列在生成的时候,当一个结点被扩展时,其所有子结点应该按照编号从小 到大的顺序访问. 思路: 一开始是想根据BF ...

  4. 【构造题 贪心】cf1041E. Tree Reconstruction

    比赛时候还是太慢了……要是能做快点就能上分了 Monocarp has drawn a tree (an undirected connected acyclic graph) and then ha ...

  5. UVa 10410 树重建

    Tree Reconstruction Problem Description You have just finished a compiler design homework question w ...

  6. UVA.548 Tree(二叉树 DFS)

    UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...

  7. Aizu - 2564 Tree Reconstruction 并查集

    Aizu - 2564 Tree Reconstruction 题意:一个有向图,要使得能确定每一条边的权值,要求是每个点的入权和出权相等,问你最少需要确定多少条边 思路:这题好像有一个定理之类的,对 ...

  8. E. Tree Reconstruction 解析(思維)

    Codeforce 1041 E. Tree Reconstruction 解析(思維) 今天我們來看看CF1041E 題目連結 題目 略,請直接看原題 前言 一開始完全搞錯題目意思,還以為每次會刪除 ...

  9. UVa 536 Tree Recovery | GOJ 1077 Post-order (习题 6-3)

    传送门1: https://uva.onlinejudge.org/external/5/536.pdf 传送门2: http://acm.gdufe.edu.cn/Problem/read/id/1 ...

随机推荐

  1. java实现折半查找

    package althorgrim;/** * 1.必须采用顺序存储结果 * 2.关键字必须有序 * @author hanrk-2734 * */public class TestBinarySe ...

  2. JAVA文件读取FileReader

    JAVA文件读取FileReader 导包import java.io.FileReader 创建构造方法public FileReader(String filename),参数是文件的路径及文件名 ...

  3. WPF通用框架ZFS《项目结构介绍01》_模块介绍

    首页介绍: 下图为项目运行首页图片, 大的结构分为三块: 1.Header首部模块(存放通知组件[全局通知.消息管理 ].扩展模块[皮肤.系统设置.关于作者.退出系统]) 2.Left左侧菜单模块(存 ...

  4. Android数据库高手秘籍(三)——使用LitePal升级表

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/39151617 在上一篇文章中,我们学习了LitePal的基本使用方法,体验了使用框 ...

  5. 第二天,导出文件sql,查询,视图view,聚合函数,反模式,字符串处理函数

    //把数据库导出到脚本文件mysqldump -uroot -p1234 --databases abc > d:/a/abc.sql CREATE TABLE stud( id INT PRI ...

  6. vue绑定内联样式

    v-bind:style 的对象语法十分直观--看着非常像 CSS ,其实它是一个 JavaScript 对象. CSS 属性名可以用驼峰式(camelCase)或短横分隔命名(kebab-case) ...

  7. QQ 5.0的一些特效学习 一

    虽然QQ5.0已经过去很久了,但是有些特效还是值得学习的 效果: 源码点我 导入的jar包, 一个是高版本的support.v4包,需要这个v4包中有ViewDragHelper. 我这里使用的是su ...

  8. 41.Node.js使用cnpm

    转自:http://www.runoob.com/nodejs/nodejs-tutorial.html npm是Node.js中维护第三方库.模块的工具,但是国外的速度很悲剧,这里有一个中国的源cn ...

  9. 关于obj.currentStyle.property、window.getComputedStyle(obj,null).property、obj.style.property的理解

    首先是obj,style.property 我一直用这个obj.style.property这个属性来修改内联和外联的obj属性,但是从网上看到了obj.style.property居然只能读取内嵌的 ...

  10. ajax模仿iframe

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...