Code the Tree(图论,树)

ZOJ Problem Set - 1097
Code the Tree Time Limit: 2 Seconds       Memory Limit: 65536 KB

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.

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 Specification

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 Specification

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.

用连接表存储树,再每次找最小的leaf即可。难点是建树。方法:

1.建一个栈用于存储节点。

2.当遇到 ( 时,输入节点编号i,(1)如果栈非空,栈顶与 i 相邻,更新邻接表中栈顶和i的相应项,再将i压入栈中;(2)如果栈为空,将i压入栈中。

3.当遇到 ) 时,弹栈。

4. 当遇到空格时,跳过。

注意当树只有根时,如(1),输出换行符即可

AC code:

 1 #include <iostream>
 2 #include <stack>
 3 #include <queue>
 4 #include <vector>
 5 #include <map>
 6 #include <list>
 7 #include <string>
 8 #include <algorithm>
 9 #include <cstdio>
10 #include <cstring>
11 #include <cmath>
12 
13 using namespace std;
14 
15 const int MAXN = 55;
16 
17 int maxId, cntId;  //maxId是最大节点,cntId是节点数
18 list<int> adj[MAXN];  //邻接表
19 
20 void build_tree()
21 {
22     char c;
23     int id;
24     stack<int> sta;
25     scanf("%d", &id);
26     sta.push(id);
27     cntId = 1;
28     maxId = id;
29     while(scanf("%c", &c) && c != '\n')
30     {
31         if(c == ' ') continue;
32         if(c == '(')
33         {
34             scanf("%d", &id);
35             cntId++;
36             if(maxId < id) maxId = id;
37             int f = sta.top();
38             adj[f].push_front(id);
39             adj[id].push_front(f);
40             sta.push(id);
41         }
42         else if(c == ')') sta.pop();
43     }
44 }
45 
46 int findMinLeaf()
47 {
48     int i;
49     for(i = 1; i <= maxId; i++)
50     {
51         if(adj[i].size() == 1) break;
52     }
53     int s = *adj[i].begin();
54     adj[i].pop_back();
55     list<int>::iterator it = adj[s].begin();
56     for(; it != adj[s].end(); it++)
57     {
58         if(*it == i) break;
59     }
60     adj[s].erase(it);
61     return s;
62 }
63 
64 int main()
65 {
66     char ch;
67     while(scanf("%c", &ch) != EOF)
68     {
69         int i;
70         for(i = 1; i < MAXN; i++)
71             adj[i].clear();
72         build_tree();
73         if(cntId < 2)
74         {
75             puts("");
76             continue;
77         }
78         for(i = 1; i < cntId - 1; i++)
79             printf("%d ", findMinLeaf());
80         printf("%d\n", findMinLeaf());
81     }
82     return 0;
83 }

2013-07-31 23:09:35

你可能感兴趣的:(Code the Tree(图论,树))