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:ios

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".ide

Input Specificationui

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.this

Output Specificationspa

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.code

用鏈接表存儲樹,再每次找最小的leaf便可。難點是建樹。方法:blog

1.建一個棧用於存儲節點。ci

2.當遇到 ( 時,輸入節點編號i,(1)若是棧非空,棧頂與 i 相鄰,更新鄰接表中棧頂和i的相應項,再將i壓入棧中;(2)若是棧爲空,將i壓入棧中。rem

3.當遇到 ) 時,彈棧。get

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

相關文章
相關標籤/搜索