UVA 10410 Tree Reconstruction
来源:互联网 发布:艾美特 暖风机知乎 编辑:程序博客网 时间:2024/05/21 17:05
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <vector>using namespace std;const int size=1010;int n,ord[size],dfs[size],bfs[size],deep[size],vis[size];vector <int> arr[size];queue <int> q;int check(int a,int b,int curDeep){int i,e=dfs[a],f=dfs[b],c=ord[e],d=ord[f];for(i=a+1;i<b;i++){int tmp=dfs[i];if(deep[tmp]==0)continue;if(deep[tmp]!=curDeep)return 0;}for(i=c+1;i<d;i++){int tmp=bfs[i];if(!vis[tmp])return 0;}return 1;}void cal(int cur,int curDeep){int cnt=dfs[cur],i;for(i=cur+1;i<n;i++){int tmp=dfs[i];if(vis[tmp])continue;if(check(cur, i, curDeep+1))arr[cnt].push_back(tmp);elsecontinue;q.push(i);vis[tmp]=1;deep[tmp]=curDeep+1;}}void print(){int i,j;for(i=1;i<=n;i++){printf("%d:",i);for(j=0;j<arr[i].size();j++)printf(" %d",arr[i][j]);puts("");}}int main(){int i,j;while(scanf("%d",&n)!=EOF){memset(vis,0,sizeof(vis));memset(deep,0,sizeof(deep));memset(ord,0,sizeof(ord));memset(dfs,0,sizeof(dfs));memset(bfs,0,sizeof(bfs));for(i=0;i<=n;i++)arr[i].clear();while(!q.empty())q.pop();for(i=0;i<n;i++){int t;scanf("%d",&t);bfs[i]=t;ord[t]=i;}for(i=0;i<n;i++)scanf("%d",&dfs[i]);deep[dfs[0]]=1;q.push(0);while(!q.empty()){int cnt=q.front();q.pop();cal(cnt,deep[dfs[cnt]]);}print();}return 0;}
1 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
- uva 10410 - Tree Reconstruction(栈)
- UVA 10410(p180)----Tree Reconstruction
- UVA 10410 - Tree Reconstruction(树)
- UVa 10410 - Tree Reconstruction(树)
- UVA 10410——Tree Reconstruction
- Tree Reconstruction UVA
- 习题6-11 树重建(Tree Reconstruction, UVa 10410)
- 10410 - Tree Reconstruction
- UVa10410 - Tree Reconstruction
- UVA10410 - Tree Reconstruction(队列)
- [UVA10410]Tree Reconstruction[BFS、DFS序建树]
- Aizu - 2564 Tree Reconstruction 并查集
- 从Java Keystore文件中提取私钥、证书
- 一次SYS用户还原普通用户失败的问题
- 用ffmpeg 把mp4文件转为ts文件并生成m3u8列表
- java实现浮点数转换成人民币读法字符串,希望大家多多指教!
- 从B 树、B+ 树、B* 树谈到R 树
- UVA 10410 Tree Reconstruction
- windows修改mac地址
- js屏蔽错误代码不让游览器显示错误提示
- checkbox 里面的value的作用
- 如何辨别茶叶里有没有添加香精
- web.xml的配置及加载顺序
- PHP获取准确的IP地址的方法
- 51nod 1182完美字符串
- Ubuntu 安装mysql和简单操作