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

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

时间:2015-05-05 19:01:07      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

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 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
AC:直接暴力枚举所有情况,每次都从从节点个数最小的为1的开始;
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<queue>
 6 #include<string>
 7 #include<cmath>
 8 using namespace std;
 9 char ss[1000];
10 int a[100][100],num[100];
11 int ans[100];
12 int main()
13 {
14  int len;
15  while(gets(ss))
16  {
17       memset(a,0,sizeof(a));
18       memset(ans,0,sizeof(ans));
19       memset(num,0,sizeof(num));
20       len = strlen(ss);
21       int aa = 0,i=0,lev=0,t=0,mmax =0,kk=0;
22         for(i = 0; i < len; i++)
23         {
24             if(ss[i] == ()
25                 lev++;
26             else if(ss[i] == ))
27                 lev--;
28             if(ss[i] >= 0 && ss[i] <= 9)
29             {
30                 t = t * 10 + ss[i] - 48;
31                 if(t > mmax)
32                     mmax = t;
33                 if(!(ss[i + 1] >= 0 && ss[i + 1] <= 9))
34                 {
35                     num[lev] = t;
36                     a[num[lev - 1]][num[lev]] = 1;
37                     a[num[lev]][num[lev - 1]] = 1;
38                 }
39             }
40             else
41             {
42                 t = 0;
43             }
44         }
45       int sum,j,k,m;
46       for( i=1;i<=mmax;i++)
47       {
48         for( j=1;j<=mmax;j++)
49         {  sum = 0;
50            for(k=1;k<=mmax;k++)//统计和他相连的数的个数
51            {
52                 sum = sum+a[j][k];
53            }
54             if(sum == 1)
55              {
56                for(m = 1;m<=mmax; m++)
57                  if(a[j][m] == 1)
58                  {
59                    ans[kk++] = m;
60                    a[j][m] = 0;
61                    a[m][j] = 0;
62                    j = 0 ;
63                     break;
64                  }
65              }
66         }
67       }
68       for(int i=0; i<kk; i++)
69         if(i == 0)printf("%d",ans[i]);
70       else printf(" %d",ans[i]);
71       printf("\n");
72  }
73   return 0;
74 }

 

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

标签:

原文地址:http://www.cnblogs.com/lovychen/p/4479550.html

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