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

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 6AC:直接暴力枚举所有情况,每次都从从节点个数最小的为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 }
时间: 2024-11-02 22:14:17

poj 2567 Code the Tree 河南第七届省赛的相关文章

POJ 2567 Code the Tree &amp;amp; POJ 2568 Decode the Tree Prufer序列

题目大意:2567是给出一棵树,让你求出它的Prufer序列.2568时给出一个Prufer序列,求出这个树. 思路:首先要知道Prufer序列.对于随意一个无根树,每次去掉一个编号最小的叶子节点,并保存这个节点所连接的节点所得到的序列就是这棵树的Prufer序列. 这个序列有十分优雅的性质.它能与无根树一一相应.因此.两个标号一样的无根树得到的Prufer序列也一定是一样的. 此外,设一个节点的度数是d[i],那么他会在Prufer序列中出现d[i] - 1次. 2567:记录每个节点的度.依

POJ 2567 Code the Tree &amp; POJ 2568 Decode the Tree Prufer序列

题目大意:2567是给出一棵树,让你求出它的Prufer序列.2568时给出一个Prufer序列,求出这个树. 思路:首先要知道Prufer序列.对于任意一个无根树,每次去掉一个编号最小的叶子节点,并保存这个节点所连接的节点所得到的序列就是这棵树的Prufer序列.这个序列有十分优雅的性质,它能与无根树一一对应.因此,两个标号一样的无根树得到的Prufer序列也一定是一样的.此外,设一个节点的度数是d[i],那么他会在Prufer序列中出现d[i] - 1次. 2567:记录每一个节点的度,按照

??第七届河南省赛B.海岛争霸(并差集)

B.海岛争霸 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 130  Solved: 48 [Submit][Status][Web Board] Description 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比海盗,你知道吧?杰克船长驾驶着自己的的战船黑珍珠1号要征服各个海岛的海盜,最后成为海盗王. 这是一个由海洋.岛屿和海盗组成的危险世界.杰克船长准备从自己所占领的岛屿A开始征程,逐个去占领每一个岛屿.面对危

第七届河南省赛H.Rectangles(lis)

10396: H.Rectangles Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 229  Solved: 33 [Submit][Status][Web Board] Description Given N (4 <= N <= 100)  rectangles and the lengths of their sides ( integers in the range 1..1,000), write a program that fi

第七届河南省赛A.物资调度(dfs)

10401: A.物资调度 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 95  Solved: 54 [Submit][Status][Web Board] Description 某地区发生了地震,灾区已经非常困难,灾民急需一些帐篷.衣物.食品和血浆等物资.可通往灾区的道路到处都是塌方,70%以上的路面损坏,桥梁全部被毁.国家立即启动应急预案,展开史上最大强度非作战空运行动,准备向灾区空投急需物资. 一方有难,八方支援.现在已知有N个地方分别

第七届省赛赛前交流赛部分题解

A题: Yougth's Game[Ⅲ]( 区间dp ) 这是在省赛前热身赛出的题目,可能是题目中有用到博弈的思想,很多人都在做,而且在尝试暴力.但是没有人往dp的方向上想. 题目类型:动态规划+博弈 分析:题意描述的很清楚,就是选择在两端取数,当前取的数不仅能够影响下一次的结果,而且能够影响后面的结果...又是一个求最优值,那么是不是可以往dp的方向上想了.区间dp 定义状态dp[ i ] [ j ] 为从 i 到 j 上A的得分,那么B的得分就是sum(i,j)-dp[ i ] [ j ]

第七届河南省赛10403: D.山区修路(dp)

10403: D.山区修路 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 69  Solved: 23 [Submit][Status][Web Board] Description 某山区的孩子们上学必须经过一条凹凸不平的土路,每当下雨天,孩子们非常艰难.现在村里走出来的Dr. Kong决定募捐资金重新修建着条路.由于资金有限,为了降低成本,对修好后的路面高度只能做到单调上升或单调下降. 为了便于修路,我们将整个土路分成了N段,每段路面的高度分

第七届河南省赛 题解&amp;&amp;题型分布

第七届省赛我还在高三,所以没有机会参加,qaq 整体来说:我会的:A 简单搜索(背包也行吧) B跟POj2253查不到 最短路(很多都是并查集写的,不知道为啥) D 动态规划(算裸的吧) F 很水的模拟 H 贪心加简单的排序 G同POJ2567也算是简单的模拟 A: 10401: A.物资调度 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 103  Solved: 59 [Submit][Status][Web Board] Descriptio

zzuli - 第七届校赛

题目传送:"玲珑杯"第七届郑州轻工业学院ACM程序设计大赛--正式赛 先给出我自己看了的题的题解吧 A - 彩票 水题 AC代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int main() { int T, N; scanf("%d", &T); while(T--) { scanf("%d&