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的开始;
 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
#include<cmath>
using namespace std;
char ss[];
int a[][],num[];
int ans[];
int main()
{
int len;
while(gets(ss))
{
memset(a,,sizeof(a));
memset(ans,,sizeof(ans));
memset(num,,sizeof(num));
len = strlen(ss);
int aa = ,i=,lev=,t=,mmax =,kk=;
for(i = ; i < len; i++)
{
if(ss[i] == '(')
lev++;
else if(ss[i] == ')')
lev--;
if(ss[i] >= '' && ss[i] <= '')
{
t = t * + ss[i] - ;
if(t > mmax)
mmax = t;
if(!(ss[i + ] >= '' && ss[i + ] <= ''))
{
num[lev] = t;
a[num[lev - ]][num[lev]] = ;
a[num[lev]][num[lev - ]] = ;
}
}
else
{
t = ;
}
}
int sum,j,k,m;
for( i=;i<=mmax;i++)
{
for( j=;j<=mmax;j++)
{ sum = ;
for(k=;k<=mmax;k++)//统计和他相连的数的个数
{
sum = sum+a[j][k];
}
if(sum == )
{
for(m = ;m<=mmax; m++)
if(a[j][m] == )
{
ans[kk++] = m;
a[j][m] = ;
a[m][j] = ;
j = ;
break;
}
}
}
}
for(int i=; i<kk; i++)
if(i == )printf("%d",ans[i]);
else printf(" %d",ans[i]);
printf("\n");
}
return ;
}

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

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

    题目大意:2567是给出一棵树,让你求出它的Prufer序列.2568时给出一个Prufer序列,求出这个树. 思路:首先要知道Prufer序列.对于随意一个无根树,每次去掉一个编号最小的叶子节点,并 ...

  2. 第七届河南省赛G.Code the Tree(拓扑排序+模拟)

    G.Code the Tree Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 35  Solved: 18 [Submit][Status][Web ...

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

    10403: D.山区修路 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 69  Solved: 23 [Submit][Status][Web Bo ...

  4. 第七届河南省赛10402: C.机器人(扩展欧几里德)

    10402: C.机器人 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 53  Solved: 19 [Submit][Status][Web Boa ...

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

    B.海岛争霸 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 130  Solved: 48 [Submit][Status][Web Board] D ...

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

    10401: A.物资调度 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 95  Solved: 54 [Submit][Status][Web Bo ...

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

    10396: H.Rectangles Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 229  Solved: 33 [Submit][Status] ...

  8. 第七届河南省赛F.Turing equation(模拟)

    10399: F.Turing equation Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 151  Solved: 84 [Submit][St ...

  9. 算法笔记_122:蓝桥杯第七届省赛(Java语言A组)试题解答

     目录 1 煤球数目 2 生日蜡烛 3 搭积木 4 分小组 5 抽签 6 寒假作业 7 剪邮票 8 取球博弈 9 交换瓶子 10 压缩变换   前言:以下试题解答代码部分仅供参考,若有不当之处,还请路 ...

随机推荐

  1. hdu5443(2015长春赛区网络赛1007)暴力

    题意:给了一个数列,有多个询问,每个询问求某个区间内的最大值 数列长度 1000,询问个数 1000,静态,并不需要RMQ这些,直接暴力 n2 查找每个询问区间取最大值就行了. #include< ...

  2. 什么是域名?什么网站名?什么是URL?

    域名,相信大家都不默认,也使用过无数次!比如: google.com.baidu.com.163.com等. 这时候,你可能要奇怪了,为什么小编没有在前面加上www? 因为正常情况下,不应该是www. ...

  3. js里的原型

    <script type="text/javascript"> function People(name){ this.name = name; //对象方法 this ...

  4. CentOS怎样强制卸载PHP以及自定义安装PHP

    很无语,CentOS居然php版本才5.1.6,很多开源的CMS无法安装. 查看php版本命令: #php -v 这个命令是删除不干净的 #yum remove php 因为使用这个命令以后再用 #p ...

  5. Java static block static constructor , static field

    http://stackoverflow.com/questions/7121213/singleton-instantiation http://docs.oracle.com/javase/spe ...

  6. PHP实现动态规划背包问题

    有一堆货物,有各种大小和价值不等的多个物品,而你只有固定大小的背包,拿走哪些能保证你的背包带走的价值最多 动态规划就是可以记录前一次递归过程中计算出的最大值,在之后的递归期间使用,以免重复计算. &l ...

  7. MyEclipse设置内存

    Tomcat直接启动正常,通过myeclipse启动tomcat内存溢出. MyEclipse启动Tomcat无视catalina.bat中设置内存大小的问题. 在 tomcat的catalina.b ...

  8. TFS 强制撤销别人签出的代码

    有个同事离职一段时间了,今天改一下她的代码,发现有个文件签出了,晕,而且TFS用的也是只允许单用户签出. 1,找原来的用的机器,已经被人占用了,系统已经重做. 2,只有用命令行来搞了. 大致如下: t ...

  9. API、ABI区别

    http://blog.csdn.net/xinghun_4/article/details/7905298 应用程序二进制接口(ABI-Application Binary Interface)定义 ...

  10. linux包之iproute之ss命令

    概述 [root@localhost ~]# rpm -qa|grep iprouteiproute-2.6.32-31.el6.x86_64 当服务器的socket连接数量变得非常大时,无论是使用n ...