POJ--2567--Code the Tree
来源:互联网 发布:java 不用tomcat 编辑:程序博客网 时间:2024/06/05 18:56
直接 模拟,然后,每次找到编号最小的一个点的父亲输出就可以了。
注意格式,输入输出就OK 了
#include<iostream>#include<cstring>#include<cstdio>#include<vector>#include<algorithm>#define maxn 10000using namespace std;char s[maxn];vector<int> g[51];int n;void init(){ for(int i=0;i<=50;i++) g[i].clear();}void build(int v,int l,int r){ char c; int u=0; int tr=0; l++;r--; int li,ri; while(l<=r) { if(s[l]>='0'&&s[l]<='9') { u=u*10+s[l]-'0'; } else if(s[l]=='(') { break; } l++; } if(u>n)n=u; if(v!=-1) { g[u].push_back(v); g[v].push_back(u); } tr=0; for(int i=l;i<=r;i++) { if(s[i]=='(') { if(tr==0)li=i; tr++; } else if(s[i]==')') { tr--; if(tr==0) { build(u,li,i); } } }}bool vis[102];void op(int num){ int fa=g[num][0]; g[fa].erase(find(g[fa].begin(),g[fa].end(),num)); vis[num]=1; printf("%d",fa);}void solve(){ int len=strlen(s); n=0; build(-1,0,len-2); memset(vis,0,sizeof(vis)); bool single_node=true; for(int i=1;i<n;i++) { single_node=false; int temp=10000; for(int j=1;j<=n;j++) { if(!vis[j]) { if(g[j].size()==1&&j<temp) { temp=j; } } } op(temp); if(i==n-1)printf("\n"); else printf(" "); } if(single_node)printf("\n");}int main(){ while(fgets(s,maxn,stdin)) { init(); solve(); } return 0;}
- poj 2567Code the Tree
- POJ 2567 Code the Tree
- POJ--2567--Code the Tree
- POJ 2567 Code the Tree & POJ 2568 Decode the Tree Prufer序列
- poj 2567 Code the Tree 【还原树 求prufer序列】
- poj 2567 zzu10395 nyoj1254 Code the Tree(Prufer数列)
- poj 2567 code thr tree
- Code the Tree
- G.Code the Tree
- ZOJ 1097 CODE the Tree
- zoj-1097-code the tree
- poj-2568 Decode the Tree
- cracking the code interview balanced tree python
- Code the Tree(记忆化深搜构造树)
- zoj 1097 Code the Tree(记忆化深搜构造树)
- nyoj1254 Code the Tree (第七届河南省程序设计大赛)
- NYOJ Code the Tree(河南省第七届省赛题目)
- TOJ Code the Tree AC 代码 以及题解
- gets() fgets()
- QML性能优化
- fedora安装gcc编译器
- C++Primer笔记 第七章 函数
- 深入探讨:LBS是一种工具而非一种模式
- POJ--2567--Code the Tree
- 从汽车共享说移动互联网的创新
- 这个被移动互联网改变的时代
- Python命名机制之浅析
- 杭电1713
- Strus2标签库属性值的%和#号的关系、命名对象 .
- 05-页面转发(jsp基础应用)
- 信号灯集实现PV操作
- Struts2的 输入校验 .