首页 技术 正文
技术 2022年11月14日
0 收藏 701 点赞 2,757 浏览 2488 个字

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. poj 2567  Code the Tree 河南第七届省赛

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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,087
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,562
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905