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

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

Source

 
应该是水题,所以连边表都没开,那set乱搞,然后就Wa了一个小时。
注意整行读入的格式:cin.getline(str,MAXL,'\n')
priority_queue的比较函数自定义:priority_queue<int,vector<int>,cmp_c> q;
还有就是普通数组储存的序列输出,在序列为空时要特判。

POJ Code the Tree 树的pufer编号的更多相关文章

  1. POJ 3321 Apple Tree(树状数组)

                                                              Apple Tree Time Limit: 2000MS   Memory Lim ...

  2. POJ 3321 Apple Tree (树状数组+dfs序)

    题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...

  3. 【POJ 1741】 Tree (树的点分治)

    Tree   Description Give a tree with n vertices,each edge has a length(positive integer less than 100 ...

  4. POJ 3321 Apple Tree 树状数组+DFS

    题意:一棵苹果树有n个结点,编号从1到n,根结点永远是1.该树有n-1条树枝,每条树枝连接两个结点.已知苹果只会结在树的结点处,而且每个结点最多只能结1个苹果.初始时每个结点处都有1个苹果.树的主人接 ...

  5. POJ 2486 Apple Tree [树状DP]

    题目:一棵树,每个结点上都有一些苹果,且相邻两个结点间的距离为1.一个人从根节点(编号为1)开始走,一共可以走k步,问最多可以吃多少苹果. 思路:这里给出数组的定义: dp[0][x][j] 为从结点 ...

  6. POJ 2486 Apple Tree ( 树型DP )

    #include <iostream> #include <cstring> #include <deque> using namespace std; #defi ...

  7. POJ 3321 Apple Tree 树状数组 第一题

    第一次做树状数组,这个东西还是蛮神奇的,通过一个简单的C数组就可以表示出整个序列的值,并且可以用logN的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...

  8. POJ 3321 Apple Tree 【树状数组+建树】

    题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...

  9. Code the Tree(图论,树)

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

随机推荐

  1. uva-10487 - Closest Sums

    暴力枚举后去重最后二分加推断找答案 #include<iostream> #include<map> #include<string> #include<cs ...

  2. rnqoj-99-配置魔药-dp

    比较好的题目~~ dp[j][k]: 第一个容器在第i秒和第二个容器在第j秒,所产生的最大魔力. if(num[i].t2<=j)dp[j][k]=max(dp[j][k],dp[num[i]. ...

  3. [ES7] Descorator: evaluated & call order

    When multiple decorators apply to a single declaration, their evaluation is similar to function comp ...

  4. [转]开源应用架构之asterisk

    作者:Russell Bryant 翻译:jiazhengfeng Asterisk[1]是一款GPLv2协议下的开源电话应用平台.简单来说,Asterisk是一个服务器应用,能够完成发起电话呼叫.接 ...

  5. cocos2d-x 2.1.4学习笔记之HelloWorld分析

    下面截图是HelloWorld项目下的文件夹结构 这是用python命令生成的项目,在创建过程中默认生成多个平台的程序文件. 1.“resource”文件夹 该文件夹主要用于存放游戏中需要的图片.音频 ...

  6. hdu2049.java

    hdu 2049 不容易系列之(4)——考新郎 (组合+错排) 国庆期间,省城HZ刚刚举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做"考新郎&qu ...

  7. 把QQ聊天记录插入数据库中

    最近在做毕设,其中一个环节是分析qq聊天记录,在分析之前需要先把qq聊天记录导出,然后存入数据库中,qq聊天记录导出后是文本文档,导出方式: 1.登录qq后,点击任意一个好友,查看与他的聊天记录,点击 ...

  8. CSS hack常用方案(摘选)

    邮箱因为默认了line-height?:170%,导致采用table元素时继承问题,可以采用line-height:50% 很好解决. 常 在使用float时,后面的显示不正常,因为继承了float了 ...

  9. 使用jquery.validate.js实现boostrap3的校验和验证

    使用jquery.validate.js实现boostrap3的校验和验证 boostrap3验证框架 jquery.validate.js校验表单 >>>>>>& ...

  10. WisDom.Net 框架设计(五) 权限设计

    WisDom.Net --权限设计 1.需求分析     基本在所有的管理系统中都离不开权限管理.可以这么说,权限管理是管理系统的核心所在. 权限管理说白一些就是每个人能够做什么,不能够做什么.可以说 ...