Code the Tree

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2292   Accepted: 878

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 obtained graph, 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)))
(6 (1 (4)) (2 (3) (5)))

Sample Output

5 2 5 2 6 2 8
2 3
2 1 6 2 6难点是  字符串的分离,   (A)根据这个特点,当读到‘(‘时,下一个一定是数字,我们遇到’)‘就结束。见代码
 1 #include"iostream"
 2 #include"cstdio"
 3 #include"map"
 4 #include"queue"
 5 #include"vector"
 6 #include"set"
 7 #include"cstring"
 8 #include"algorithm"
 9 using namespace std;
10 void solve(vector< set<int> > &v,int p=0)
11 {
12     int x;
13     cin>>x;
14     if(p)
15     {
16         v[x].insert(p);
17         v[p].insert(x);
18     }
19     while(1)
20     {
21         char ch;
22         cin>>ch;
23         if(ch==‘)‘)
24             break;
25         solve(v,x);
26     }
27     return ;
28 }
29 int main()
30 {
31     int i,j,n,k;
32     char ch;
33     while(cin>>ch)
34     {
35         vector< set<int> > vec(1024,set<int>());
36         priority_queue<int,vector<int>,greater<int> > que;
37         n=0;
38         solve(vec);
39         for(i=1;i<vec.size();i++)
40         {
41             if(vec[i].size())
42             {
43                 n++;
44                 if(vec[i].size()==1)
45                     que.push(i);
46             }
47         }
48         for(i=1;i<n;i++)
49         {
50             int t=que.top();
51             que.pop();
52             int p=*vec[t].begin();
53             if(i>1)
54                 printf(" ");
55             printf("%d",p);
56             vec[p].erase(t);
57             if(vec[p].size()==1)
58                 que.push(p);
59         }
60         printf("\n");
61     }
62     return 0;
63 }
				
时间: 2024-08-24 07:54:32

Code the Tree的相关文章

#Leet Code# Unique Tree

语言:Python 描述:使用递归实现 1 class Solution: 2 # @return an integer 3 def numTrees(self, n): 4 if n == 0: 5 return 0 6 elif n == 1: 7 return 1 8 else: 9 part_1 = self.numTrees(n-1) * 2 10 part_2 = 0 11 12 for i in range(1,n-1): 13 part_left = self.numTrees(

POJ Code the Tree 树的pufer编号

Code the Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2259   Accepted: 859 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

#Leet Code# Same Tree

语言:Python 描述:使用递归实现 1 # Definition for a binary tree node 2 # class TreeNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.left = None 6 # self.right = None 7 8 class Solution: 9 # @param p, a tree node 10 # @param q, a tree node 11 # @return

nyoj1254 Code the Tree (第七届河南省程序设计大赛)

题目1254 题目信息 运行结果 本题排行 讨论区 Code the Tree 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 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

poj 2567 Code the Tree 河南第七届省赛

Code the Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2350   Accepted: 906 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

POJ 2567 Code the Tree &amp;amp; POJ 2568 Decode the Tree Prufer序列

题目大意:2567是给出一棵树,让你求出它的Prufer序列.2568时给出一个Prufer序列,求出这个树. 思路:首先要知道Prufer序列.对于随意一个无根树,每次去掉一个编号最小的叶子节点,并保存这个节点所连接的节点所得到的序列就是这棵树的Prufer序列. 这个序列有十分优雅的性质.它能与无根树一一相应.因此.两个标号一样的无根树得到的Prufer序列也一定是一样的. 此外,设一个节点的度数是d[i],那么他会在Prufer序列中出现d[i] - 1次. 2567:记录每个节点的度.依

POJ 2567 Code the Tree &amp; POJ 2568 Decode the Tree Prufer序列

题目大意:2567是给出一棵树,让你求出它的Prufer序列.2568时给出一个Prufer序列,求出这个树. 思路:首先要知道Prufer序列.对于任意一个无根树,每次去掉一个编号最小的叶子节点,并保存这个节点所连接的节点所得到的序列就是这棵树的Prufer序列.这个序列有十分优雅的性质,它能与无根树一一对应.因此,两个标号一样的无根树得到的Prufer序列也一定是一样的.此外,设一个节点的度数是d[i],那么他会在Prufer序列中出现d[i] - 1次. 2567:记录每一个节点的度,按照

Code the Tree ZOJ - 1097

传送门:https://vjudge.net/problem/ZOJ-1097 解题思路: 主要是找到每一节点的相邻节点,然后后面的用优先队列就行了. #include <cstdio> #include <queue> #include <vector> #include <set> #include <iostream> using namespace std; void parse(vector<set<int> >

cmockery库详解

1.cmockery库源码的编译链接 下载链接:https://code.google.com/p/cmockery/downloads/list 下载需要FQ,没有条件的可以直接在github上找一篇比较完整的版本. $ ./configure $ make clean && make $ make install prefix=~/share/code/cmockery $ tree ~/share/code/cmockery install ├── include │ └── goo