博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Code the Tree
阅读量:5796 次
发布时间:2019-06-18

本文共 4140 字,大约阅读时间需要 13 分钟。

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2292   Accepted: 878

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 82 32 1 6 2 6 难点是  字符串的分离,   (A)根据这个特点,当读到‘('时,下一个一定是数字,我们遇到’)‘就结束。见代码
1 #include"iostream" 2 #include"cstdio" 3 #include"map" 4 #include"queue" 5 #include"vector" 6 #include"set" 7 #include"cstring" 8 #include"algorithm" 9 using namespace std;10 void solve(vector< set
> &v,int p=0)11 {12 int x;13 cin>>x;14 if(p)15 {16 v[x].insert(p);17 v[p].insert(x);18 }19 while(1)20 {21 char ch;22 cin>>ch;23 if(ch==')')24 break;25 solve(v,x);26 }27 return ;28 }29 int main()30 {31 int i,j,n,k;32 char ch;33 while(cin>>ch)34 {35 vector< set
> vec(1024,set
());36 priority_queue
,greater
> que;37 n=0;38 solve(vec);39 for(i=1;i
1)54 printf(" ");55 printf("%d",p);56 vec[p].erase(t);57 if(vec[p].size()==1)58 que.push(p);59 }60 printf("\n");61 }62 return 0;63 }

 

1 #include"iostream" 2 #include"cstdio" 3 #include"map" 4 #include"queue" 5 #include"vector" 6 #include"set" 7 #include"cstring" 8 #include"algorithm" 9 using namespace std;10 void solve(map
> &m,int p=0)11 {12 int x;13 cin>>x;14 if(p)15 {16 m[p].insert(x);17 m[x].insert(p);18 }19 while(1)20 {21 char ch;22 cin>>ch;23 if(ch==')')24 break;25 solve(m,x);26 }27 return ;28 }29 int main()30 {31 int i,j,n,k;32 char ch;33 while(cin>>ch)34 {35 priority_queue
,greater
> que;36 map
> mp;37 solve(mp);38 for(i=1;i<=mp.size();i++)39 {40 if(mp[i].size()==1)41 {42 que.push(i);43 }44 }45 for(i=1;i
1)51 printf(" ");52 printf("%d",p);53 mp[p].erase(t);54 if(mp[p].size()==1)55 que.push(p);56 }57 printf("\n");58 }59 return 0;60 }

 

 

转载于:https://www.cnblogs.com/767355675hutaishi/p/4007059.html

你可能感兴趣的文章
C++程序的设计机制3 RAII机制(2)
查看>>
ThinkPHP5基础学习(慕课版)
查看>>
iOS开发之--隐藏状态栏
查看>>
15款优秀移动APP产品原型设计工具
查看>>
SharePoint PowerShell命令系列 (12) New-SPWebApplication
查看>>
python的select服务端的代码和客户端的代码
查看>>
学习总结计划
查看>>
DFS HDU 5305 Friends
查看>>
C# 使用 Socket 实现 http 协议全功能版
查看>>
构建NetCore应用框架之实战篇(五):BitAdminCore框架1.0登录功能设计实现及源码...
查看>>
GC算法 垃圾收集器
查看>>
linux 配置tomcat服务器
查看>>
点点滴滴的生活
查看>>
马云不想成为“马云”
查看>>
MRP工作台任务下达之计划组为必输
查看>>
完工原因栏必输
查看>>
android进程与线程详解
查看>>
为C#自定义控件添加自定义事件
查看>>
WIN8.1的安装和打开"这台电脑"速度很慢的解决办法
查看>>
SPFA算法模板
查看>>