Code the Tree
-
描述
-
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".
-
输入
- 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 输出
-
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)))
样例输出
-
5 2 5 2 6 2 82 3
来源
- 第七届河南省程序设计大赛 上传者
- 516108736
分析:
难在题目的理解和字符的处理
题意:
根据字符生成一个图(看成图而不是树更好理解吧),然后把只有一条边的最小顶点对应的父结点输出出来,删除点和边。
比如样例2,
点1和点3都只连了1条边,点1,较小,输出2,删除点1和对应边
此时点2,和3都只连了1条边,点2,较小,输出3,删除点2和对应边
此时只剩下一个点,满足“this procedure is repeated, until there is only one vertex left”,结束。
字符处理:
当遇到数字时,入栈,当遇到'('和' '时,不用管,当遇到')'时
出栈一个元素,把它和栈顶元素建立关系,注意,最后一个')'是多余的,循环时循环到i<len-1即可,不然会使得空栈做出栈操作
然后就是vector的使用了,各种方法记起来~
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<stack>
#include<map>
#include<vector>
using namespace std;
int main(){
string s;
int m1,m2;
while(getline(cin,s)){
int n=0;
vector<int> rel[55];
stack<int> shu;
int len=s.length();
for(int i=0;i<len-1;){
if(s[i]<='9'&&s[i]>='0'&&i<len){
int t1=0,t2=10;
while(s[i]<='9'&&s[i]>='0'&&i<len){
t1=t1*t2+s[i]-'0';
i++;
}
n++;
shu.push(t1);
}
if(s[i]==' '||s[i]=='(')i++;
if(s[i]==')'){
m1=shu.top();shu.pop();
m2=shu.top();
rel[m1].push_back(m2);
rel[m2].push_back(m1);
i++;
}
}
for(int i=1;i<=n;i++){
sort(rel[i].begin(),rel[i].end());
}
int q=n-2,t1;
for(int i=1;i<=n;i++){
if(rel[i].size()==1){
t1=rel[i][0];
printf("%d",t1);
rel[i].resize(0);
vector<int>::iterator iter;
iter=lower_bound(rel[t1].begin(),rel[t1].end(),i);
rel[t1].erase(iter);
break;
}
}
while(q--){//每次删除一个叶子结点
for(int i=1;i<=n;i++){
if(rel[i].size()==1){
t1=rel[i][0];
printf(" %d",t1);
rel[i].resize(0);
vector<int>::iterator iter;
iter=lower_bound(rel[t1].begin(),rel[t1].end(),i);
rel[t1].erase(iter);
break;
}
}
}
printf("\n");
}
}