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

Prufer编码解析
本文介绍了一种基于树形结构的Prufer编码方法,并通过示例解释了如何生成Prufer码。此外,还提供了一段C++代码用于演示如何实现这一过程。
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 }

 

转载于:https://www.cnblogs.com/lovychen/p/4479550.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值