码迷,mamicode.com
首页 > 其他好文 > 详细

Code the Tree

时间:2014-10-05 16:49:58      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   for   

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.bubuko.com,布布扣

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 }

 



Code the Tree

标签:des   style   blog   http   color   io   os   ar   for   

原文地址:http://www.cnblogs.com/767355675hutaishi/p/4007059.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!