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. nodejs学习笔记之mongoDB

    这两天在学习nodejs,但是发现那本书nodejs入门指南上所用的好多方法都报错. 这里主要说下数据库部分 关于注册部分:书上创建数据库那里可能要小心点,用户名不存在的时候,下面调用save的对象要 ...

  2. CSS计算样式的获取

    一般来说我们获取CSS的样式的时候会优先采用Elment.style.cssName 这种方法,这种方法类似于对象设置get,set属性获取,例如Elment.style.cssName是获取,Elm ...

  3. 多元线性回归(Linear Regression with multiple variables)与最小二乘(least squat)

    1.线性回归介绍 X指训练数据的feature,beta指待估计得参数. 详细见http://zh.wikipedia.org/wiki/%E4%B8%80%E8%88%AC%E7%BA%BF%E6% ...

  4. [Java Code] 时间维度循环生成代码片段

    public static void main(String[] args) throws ParseException { String str = "20140301"; St ...

  5. JavaScript如何判断参数为浮点型

    在codewars里,确实可以学到很多很酷的方法,例如这一次的题目是判断数字是否为浮点型.我一开始是想有没有原生的js方法,像isNaN(),isFinite(),在前者Infinity是不属于NaN ...

  6. Long与long的比较

    Java中如果使用 == 双等于比较对象,等于比较的是两个对象的内存地址,也就是比较两个对象是否是同一个对象如果比较两个Long对象值是否相等,则不可以使用双等号进行比较,可以采用如下方式:1. 使用 ...

  7. ios 调试

    http://www.cnblogs.com/weilaikeji/p/3306597.html http://www.cnblogs.com/Twisted-Fate/p/4760156.html ...

  8. OpenNebula Restfull 接口请求示例

    Fri Jun 20 07:28:20 2014 [I]: 10.0.2.2 - - [20/Jun/2014 07:28:20] "POST /vmtemplate HTTP/1.1&qu ...

  9. JSF教程(10)——生命周期之Update Model Values Phase

    在整个JSF生命周期中经历了取值.验证的阶段终于从request中拿到合理的值,以下就是在本阶段给相应的服务端对象(ManageBean)赋值了.JSF实现仅仅是去更新和input组件中value属性 ...

  10. 关于2000W数据

    前几天在博客园首页看到这个2000W数据的消息,刚好这个学期正在SQL入门,加上好奇心的驱使,把这个下载下来. 一个是600多M的CSV文件,还有一个是1.7G的SQL Server的备份文件,解压后 ...