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

【题意】

在这里输入题意

【题解】

可以先确定当前这棵子树的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. JS对象继承与原型链

    1.以复制方式实现的继承 1.1浅拷贝 基本类型的复制 var parent = { lanage: "chinese" } var child = { name: "x ...

  2. Windows10 Linux子系统的启用和中文用户名的修改

    一直用的虚拟机Linux,忽然心血来潮,看到Windows 10可以使用Linux子系统,于是来装一波,按照这位前辈的教程 https://blog.csdn.net/zhangdongren/art ...

  3. vue踩坑-This dependency was not found

    * vux in ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&i ...

  4. SpringBoot 整合 Mybatis 和 Mysql (详细版)

    结构如下 1.引入相关依赖 <!--mysql--><dependency> <groupId>mysql</groupId> <artifact ...

  5. 安装Apache PHP MySQL PHPMyAdmin

    视频教程:https://www.youtube.com/watch?v=FJC2iGt_2bc,Youtube看不了的FQ吧-3- 本人参考这篇文章:http://blog.csdn.net/kno ...

  6. 洛谷 P3913 车的攻击

    P3913 车的攻击 题目描述 N \times NN×N 的国际象棋棋盘上有KK 个车,第ii个车位于第R_iRi​行,第C_iCi​ 列.求至少被一个车攻击的格子数量. 车可以攻击所有同一行或者同 ...

  7. Swift编程语言初探

    继WWDC2014后,新的编程语言Swift浮出水面.它具有高速.现代.安全.可交互等特征,而且其语法简单,入门门槛低,有望替代语法复杂难懂的Objective-C语言.据其作者Chris Lattn ...

  8. USACO milk

    /* ID:kevin_s1 PROG:milk LANG:C++ */ #include <iostream> #include <string> #include < ...

  9. Cocos2d-x学习笔记(20)(TestCpp源代码分析-4)

    本章主要介绍testResource.h与tests.h,当中tests.h主要是存放全部用到的头文件.与菜单相相应的宏定义以及菜单数组,testResource.h主要用是资源文件定义. //tes ...

  10. zabbix3.4.7搭建及邮件告警

    Zabbix3.4.7部署 系统环境:CentOs7.2 1.关闭selinux 1.1 [root@localhost ~]# setenforce 0 #临时关闭 1.2 [root@localh ...