用bfs和dfs建树--uva10410 Tree reconstruction
来源:互联网 发布:淘宝不能提交订单了 编辑:程序博客网 时间:2024/06/16 19:09
将bfs作为点的顺序。
#include <iostream>
#include <vector>
#include <cstdio>
using namespacestd;
const int maxn =1000 + 5;
int pos[maxn],dfs[maxn];
vector<int> mp[maxn];
int main()
{
int n;
while(scanf("%d",&n) !=EOF)
{
int k;
for (int i =0; i < n; i ++) {
scanf("%d",&k);
pos[k] = i;
mp[i].clear();
}
scanf("%d",&dfs[0]);
int cnt =0,p;
for (int i =1; i < n; i ++) {
scanf("%d",&p);
while (cnt &&pos[p] <= pos[dfs[cnt]] +1) {
//如果dfs[cnt]这个结点+1在bfs中的顺序比下一个结点p大,说明他们是兄弟关系,需要cnt--,在dfs中前移找他的根节点
cnt --;
}
mp[dfs[cnt] -1].push_back(p);
dfs[++cnt] = p;
}
for (int i =0; i < n; i ++) {
printf("%d:",i +1);
for (int j =0; j < mp[i].size(); j ++) {
printf(" %d",mp[i][j]);
}
printf("\n");
}
}
return0;
}
- 用bfs和dfs建树--uva10410 Tree reconstruction
- [UVA10410]Tree Reconstruction[BFS、DFS序建树]
- uva10410 - Tree Reconstruction BFS和DFS重建树
- UVa10410 - Tree Reconstruction
- UVA10410 - Tree Reconstruction(队列)
- uva10410(树还原BFS)
- Tree: DFS vs BFS
- Tree -- Traverse & DFS & BFS
- UVa 548 Tree 根据后序遍历和中序遍历建树后DFS
- pat甲级 1020. Tree Traversals map递归建树 + BFS
- pat 1127. ZigZagging on a Tree (30) 递归建树 + BFS
- uva 548 Tree(通过后序,先序重建树+dfs)
- 【Codeforces 343D】Water Tree dfs序建树+线段树
- CODEFORCES 430E Guess the Tree <dfs搜索建树>
- Uva548 Tree 【递归建树+dfs遍历树】【例题6-8】
- 复习一下dfs和bfs 用java
- 用二叉树区分DFS和BFS
- DFS和BFS
- python2.7编码
- 深入理解JVM(三)——垃圾收集策略详解
- Oracle & SQLServer 脚本批量执行方法
- 从python的random函数说起
- okhttp
- 用bfs和dfs建树--uva10410 Tree reconstruction
- Oracle 批量获得数据库中表字段,类型说明,列注释等
- Linux网络编程
- 系统开发总结教训1
- 创建c++动态链接库
- 13.2-全栈Java笔记:打飞机游戏实战项目|Graphics|ImageIO|GameUtil
- 记录比较好的参考文章网址
- Random函数
- ActiveMQ死信队列使用