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 8
2 3
2 1 6 2 6
难点是 字符串的分离, (A)根据这个特点,当读到‘('时,下一个一定是数字,我们遇到’)‘就结束。见代码
 #include"iostream"
#include"cstdio"
#include"map"
#include"queue"
#include"vector"
#include"set"
#include"cstring"
#include"algorithm"
using namespace std;
void solve(vector< set<int> > &v,int p=)
{
int x;
cin>>x;
if(p)
{
v[x].insert(p);
v[p].insert(x);
}
while()
{
char ch;
cin>>ch;
if(ch==')')
break;
solve(v,x);
}
return ;
}
int main()
{
int i,j,n,k;
char ch;
while(cin>>ch)
{
vector< set<int> > vec(,set<int>());
priority_queue<int,vector<int>,greater<int> > que;
n=;
solve(vec);
for(i=;i<vec.size();i++)
{
if(vec[i].size())
{
n++;
if(vec[i].size()==)
que.push(i);
}
}
for(i=;i<n;i++)
{
int t=que.top();
que.pop();
int p=*vec[t].begin();
if(i>)
printf(" ");
printf("%d",p);
vec[p].erase(t);
if(vec[p].size()==)
que.push(p);
}
printf("\n");
}
return ;
}
 #include"iostream"
#include"cstdio"
#include"map"
#include"queue"
#include"vector"
#include"set"
#include"cstring"
#include"algorithm"
using namespace std;
void solve(map<int,set<int> > &m,int p=)
{
int x;
cin>>x;
if(p)
{
m[p].insert(x);
m[x].insert(p);
}
while()
{
char ch;
cin>>ch;
if(ch==')')
break;
solve(m,x);
}
return ;
}
int main()
{
int i,j,n,k;
char ch;
while(cin>>ch)
{
priority_queue<int,vector<int>,greater<int> > que;
map<int,set<int> > mp;
solve(mp);
for(i=;i<=mp.size();i++)
{
if(mp[i].size()==)
{
que.push(i);
}
}
for(i=;i<mp.size();i++)
{
int t=que.top();
que.pop();
int p=*mp[t].begin();
if(i>)
printf(" ");
printf("%d",p);
mp[p].erase(t);
if(mp[p].size()==)
que.push(p);
}
printf("\n");
}
return ;
}
												

Code the Tree的更多相关文章

  1. Code the Tree(图论,树)

    ZOJ Problem Set - 1097 Code the Tree Time Limit: 2 Seconds      Memory Limit: 65536 KB A tree (i.e. ...

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

    Code the Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2350   Accepted: 906 Desc ...

  3. POJ Code the Tree 树的pufer编号

    Code the Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2259   Accepted: 859 Desc ...

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

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

  5. #Leet Code# Same Tree

    语言:Python 描述:使用递归实现 # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # ...

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

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

  7. #Leet Code# Binary Tree Max[待精简]

    描述:递归调用,getMax返回 [节点值,经过节点左子节点的最大值,经过节点右节点的最大值],每次递归同时查看是否存在不经过节点的值大于max. 代码:待优化 def getLargeNode(se ...

  8. #Leet Code# Unique Tree

    语言:Python 描述:使用递归实现 class Solution: # @return an integer def numTrees(self, n): : elif n == : else: ...

  9. 20162314 Experiment 2 - Tree

    Experiment report of Besti course:<Program Design & Data Structures> Class: 1623 Student N ...

随机推荐

  1. Java 性能分析工具

    如何利用 JConsole观察分析Java程序的运行,进行排错调优 http://jiajun.iteye.com/blog/810150 如何使用JVisualVM进行性能分析 http://jia ...

  2. CentOS 7 最小化安装之后安装Mysql

    启动网卡 验证网络管理器和网卡接口状态 # systemctl status NetworkManager.service # nmcli dev status 修改网卡配置文件 通过上一步可以看到有 ...

  3. 快速备份sqlserver2005以上版本数据库的方法-摘自网络

    使用优化参数的备份命令 BACKUP DATABASE [test]TO DISK = N'D:\test_FULL_20110311_050001_1.bak',DISK = N'D:\test_F ...

  4. 对JDK,JRE,JVM的理解

    JAVA用到现在还是分不太清楚JDK,JRE,JVM这三者的区别与联系,一直都是模模糊糊的.所以今天整理下此中的关系. 简单说明:我们编写的.java文件经过JDK(JDK的bin目录下javac.e ...

  5. 报表服务框架:WEB前端UI

    1.Highcharts 2.ECharts 3.ichartjs 参考: http://v1.hcharts.cn/index.php http://echarts.baidu.com/ http: ...

  6. Xcode 4 插件制作入门

    转自:http://www.onevcat.com/2013/02/xcode-plugin/ 2014.5.4更新 对于 Xcode 5,本文有些地方显得过时了.Xcode 5 现在已经全面转向了 ...

  7. Cognos报表打开参数

    查看门户页面 http://localhost:9300/p2pd/servlet/dispatch? b_action=xts.run &m=portal/cc.xts &gohom ...

  8. poj 1679 http://poj.org/problem?id=1679

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  9. [iOS基础控件 - 6.9] 聊天界面Demo

    A.需求 做出一个类似于QQ.微信的聊天界面 1.每个cell包含发送时间.发送人(头像).发送信息 2.使用对方头像放在左边,我方头像在右边 3.对方信息使用白色背景对话框,我方信息使用蓝色背景对话 ...

  10. Linux df 命令用法示例

    介绍: Linux中df命令可以用来显示目前在Linux系统上的文件系统的磁盘使用情况统计.这些工具可以方便地知道哪些文件系统消耗多少内存.此外,如果被拾起,并且提供一个特定的文件名作为df命令的参数 ...