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. HDU-5792 World is Exploding(树状数组)

    题目大意:给一个整数序列,统计四元组(a,b,c,d)的个数,满足条件1:a<>b<>c<>d:条件2:<a,b>组成一个顺序对,<c,d> ...

  2. checkbox 点击全选

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. MFC中使用Duilib--1

    网上找到Duilib入门教程中,第一个给的时基于SDK的例子,在这里,自己写了个MFC的,与入门教程中的例子一样. 新建一个窗口类(CTestDlg) TestDlg.h内容如下: [cpp] vie ...

  4. ServiceStack

    https://github.com/ServiceStack/ServiceStack/wiki/Routing Service Gateway https://github.com/Service ...

  5. jquery mouseout事件错误(bug)

    移到子元素上时(例如,处在div中的图像),触发移出事件 (mouseout事件的一个常见错误). 解决办法是使用hover事件 在使用hover事件前,我抓耳挠腮的以为是margin或padding ...

  6. javaScript call 函数的用法说明

     call 方法  请参阅 应用于:Function 对象 要求 版本 5.5 调用一个对象的一个方法,以另一个对象替换当前对象. call([thisObj[,arg1[, arg2[, [,.ar ...

  7. mysql 索引过长1071-max key length is 767 byte

    问题 create table: Specified key was too long; max key length is 767 bytes   原因 数据库表采用utf8编码,其中varchar ...

  8. HttpClient简介 post get -转自ibm

    HttpClient简介 HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 jav ...

  9. C#子线程刷新界面并关闭窗体

    目的:要循环刷新界面上的控件,同时不影响用户操作.循环结束后关闭窗体. 步骤:先创建一个窗体,窗体中拖入一个lable控件(label1),一个button控件(button1) 代码窗口输入: // ...

  10. jsp中的内置对象(9个)、作用

    jsp内置对象 定义:可以不加声明就在JSP页面脚本(Java程序片和Java表达式)中使用的成员变量 JSP共有以下9种基本内置组件(可与ASP的6种内部组件相对应): 1.request对象 客户 ...