Tree Reconstruction UVA
来源:互联网 发布:sql have count 编辑:程序博客网 时间:2024/06/03 17:27
利用数组,首先记录BFS时每个节点的位置,然后利用栈,首先压入根节点的位置,对于后面读取的每个节点,如果是当前节点的子节点,那么位置之差至少为2(只考虑这种简单的情况,因为题目当中指出只要输出正确的情况之一即可),或者栈顶所表示的节点是根节点,那么就可以忽略位置的差值。同时注意,产生的树不一定必须是二叉树。具体实现见如下代码:
#include<iostream>#include<vector>#include<string>#include<set>#include<stack>#include<queue>#include<map>#include<algorithm>#include<cmath>#include<iomanip>#include<cstring>#include<sstream>#include<cstdio>#include<deque>using namespace std;int main(){int n;while (cin >> n){int bfs[1010];int dfs[1010];vector<int> res[1010];int root = 0;for (int i = 1; i <= n; i++){int x;cin >> x;bfs[x] = i;}cin >> root;stack<int> s;s.push(root);for (int i = 1; i < n; i++){int x;cin >> x;while (true){int a = s.top();if (a == root || bfs[x] > bfs[a] + 1){s.push(x);res[a].push_back(x);break;}else s.pop();}}for (int i = 1; i <= n; i++){cout << i << ":";for (int j = 0; j < res[i].size(); j++){cout << " " << res[i][j];}cout << endl;}}return 0;}
阅读全文
0 0
- UVA - 10410 Tree Reconstruction
- Uva - 10410 - Tree Reconstruction
- uva 10410 - Tree Reconstruction
- uva 10410 - Tree Reconstruction
- Uva-10410 Tree Reconstruction
- UVA 10410 Tree Reconstruction
- Tree Reconstruction UVA
- uva 10410 - Tree Reconstruction(栈)
- UVA 10410(p180)----Tree Reconstruction
- UVA 10410 - Tree Reconstruction(树)
- UVa 10410 - Tree Reconstruction(树)
- UVA 10410——Tree Reconstruction
- 习题6-11 树重建(Tree Reconstruction, UVa 10410)
- 10410 - Tree Reconstruction
- UVa10410 - Tree Reconstruction
- UVA10410 - Tree Reconstruction(队列)
- [UVA10410]Tree Reconstruction[BFS、DFS序建树]
- Aizu - 2564 Tree Reconstruction 并查集
- SpringMvc+POI 处理Excel的导入操作(一般form提交和jquery.form.js提交)
- Tensorflow安装-Anacoda
- 算法-->级数
- 学习SPI的一些疑惑
- Tablayout+Fragment+侧拉 一些简单的实现
- Tree Reconstruction UVA
- 位运算
- java.lang.NoClassDefFoundError: org/apache/http/ssl/TrustStrategy
- Func<T> 委托
- spring boot 打包成jar 在服务器上运行
- ARKit从入门到精通(5)-ARScnView介绍
- C++遍历文件夹下的所有文件
- 在ROS中与其他器件使用十六进制串口通信
- 关于play从jdk1.6换到jdk1.8时报错问题的解决办法