G.Code the Tree
来源:互联网 发布:网络上豆汁是什么意思 编辑:程序博客网 时间:2024/06/05 17:56
Description
A tree (i.e. a connected graph without cycles) with vertices numbered by the integers 1, 2, ..., n is given. The "Prufer" code of such a tree is built as follows: the leaf (a vertex that is incident to only one edge) with the minimal number is taken. This leaf, together with its incident edge is removed from the graph, while the number of the vertex that was adjacent to the leaf is written down. In the new obtained tree, this procedure is repeated, until there is only one vertex left (which, by the way, always has number n). The written down sequence of n-1 numbers is called the Prufer code of the tree. Your task is, given a tree, to compute its Prufer code. The tree is denoted by a word of the language specified by the following grammar:
T ::= "(" N S ")"
S ::= " " T S | empty
N ::= number
That is, trees have parentheses around them, and a number denoting the identifier of the root vertex, followed by arbitrarily many (maybe none) subtrees separated by a single space character. As an example, take a look at the tree in the figure below which is denoted in the first line of the sample input. To generate further sample input, you may use your solution to Problem 2568. Note that, according to the definition given above, the root of a tree may be a leaf as well. It is only for the ease of denotation that we designate some vertex to be the root. Usually, what we are dealing here with is called an "unrooted tree".
Input
The input contains several test cases. Each test case specifies a tree as described above on one line of the input file. Input is terminated by EOF. You may assume that 1<=n<=50
Output
For each test case generate a single line containing the Prufer code of the specified tree. Separate numbers by a single space. Do not print any spaces at the end of the line.
Sample Input
(2 (6 (7)) (3) (5 (1) (4)) (8))
(1 (2 (3)))
Sample Output
5 2 5 2 6 2 8
2 3
主要是题意,了解题意就很简单,给出一组广义表,得到图形表示,每次去掉只有一条边相连的点(值最小的那一个)输出和它相连的那个点并将这条边去掉与2相连的最开始有6,3,5,4,8与6相连有2,7与3相连只有2与5相连有1,2,4与1相连只有5,与4相连只有5,与7相连只有6与8相连只有2所以最后答案 5 2 5 2 6 2 8#include<bits/stdc++.h>using namespace std;int main(){ char st[10000]; while(gets(st)!=NULL) { stack<int>qi; vector<int>a[55]; int ma=-1; for(int i=0;i<strlen(st);i++) { if(st[i]=='(')//判断是否是边的其中一端 { int s=0; i++; while(st[i]>='0'&&st[i]<='9') { s=s*10+st[i]-'0'; i++; } ma=max(ma,s);//确定长度 if(!qi.empty())//判断是否已经有一个端点 { int l=qi.top(); a[l].push_back(s); a[s].push_back(l); } qi.push(s);端点入栈 i--; } if(st[i]==')')//该端点的所有边结束出栈 qi.pop(); } int b[55]; int t=0; for(int i=1;i<ma;i++) { for(int j=1;j<=ma;j++) { if(a[j].size()==1) { int sh=a[j][0]; b[t]=a[j][0]; t++; for(int k=0;k<a[sh].size();k++) if(a[sh][k]==j)//取端点,去边 { int l=a[sh][k]; a[sh][k]=a[sh][a[sh].size()-1]; a[sh][a[sh].size()-1]=l; a[sh].pop_back(); } a[j].pop_back(); break; } } } printf("%d",b[0]); for(int i=1;i<t;i++) { printf(" %d",b[i]); } printf("\n"); } }
0 0
- G.Code the Tree
- Code the Tree
- 第七届河南省ACM大学生程序设计大赛 G.Code The Tree
- 第七届河南省赛zzuoj 10395: G.Code the Tree (模拟)prufer数列
- poj 2567Code the Tree
- ZOJ 1097 CODE the Tree
- POJ 2567 Code the Tree
- POJ--2567--Code the Tree
- zoj-1097-code the tree
- cracking the code interview balanced tree python
- Code the Tree(记忆化深搜构造树)
- g-code
- zoj 1097 Code the Tree(记忆化深搜构造树)
- poj 2567 Code the Tree 【还原树 求prufer序列】
- nyoj1254 Code the Tree (第七届河南省程序设计大赛)
- NYOJ Code the Tree(河南省第七届省赛题目)
- poj 2567 zzu10395 nyoj1254 Code the Tree(Prufer数列)
- TOJ Code the Tree AC 代码 以及题解
- hbase 二级索引
- 【带修改莫队】BZOJ2120 数颜色
- Kettle部署集群
- MySQL模糊查询
- 计算机网络(第六版)
- G.Code the Tree
- 笔试面试算法经典--最长括号匹配
- gg
- python模块学习
- Jquery懒加载插件
- Android使用SVG小结
- ReactNative text居中问题,和报color 错误问题 ios和android
- huffman树【严蔚敏】
- SPRING中value和ref的简写形式