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. android string[] arraylist<string>互转

    string[] to arraylist<string>:eg:string[] str; arraylist<string> arr; for(int i=0;i<s ...

  2. [Hive - LanguageManual] VirtualColumns

    Virtual Columns Simple Examples Virtual Columns Hive 0.8.0 provides support for two virtual columns: ...

  3. Navicate

    快捷键 1.ctrl+q 打开查询窗口 2.ctrl+/ 注释sql语句 3.ctrl+shift +/ 解除注释 4.ctrl+r 运行查询窗口的sql语句 5.ctrl+shift+r 只运行选中 ...

  4. 第三百零六天 how can I 坚持

    今天做了件并不是我风格的事,送了张公交卡,还没送出去,好难搞啊.这天会铭记的.如果将来我们能走在一起. 中午去朝阳门拿了我的荣事达破壁机,好大啊,怎么带回家啊,还有,回家要不要买两只烤鸭啊. 今天聊了 ...

  5. Spark SQL概念学习系列之如何使用 Spark SQL(六)

    val sqlContext = new org.apache.spark.sql.SQLContext(sc) // 在这里引入 sqlContext 下所有的方法就可以直接用 sql 方法进行查询 ...

  6. Spark RDD概念学习系列之Spark的算子的作用(十四)

    Spark的算子的作用 首先,关于spark算子的分类,详细见 http://www.cnblogs.com/zlslch/p/5723857.html 1.Transformation 变换/转换算 ...

  7. 【转】你真的了解iOS代理设计模式吗?

    转自:http://www.cocoachina.com/ios/20160317/15696.html 在项目中我们经常会用到代理的设计模式,这是iOS中一种消息传递的方式,也可以通过这种方式来传递 ...

  8. JSF 2 dropdown box example

    In JSF, <h:selectOneMenu /> tag is used to render a dropdown box – HTML select element with &q ...

  9. JSF 2.0 + Ajax hello world example

    In JSF 2.0, coding Ajax is just like coding a normal HTML tag, it's extremely easy. In this tutorial ...

  10. [iOS UI进阶 - 5.0] 手势解锁Demo

    A.需求 1.九宫格手势解锁 2.使用了绘图和手势事件   code source: https://github.com/hellovoidworld/GestureUnlockDemo     B ...