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. Android实战技巧之六:PreferenceActivity使用详解

    一.写作前面 当我们做应用的时候,需要用户配置一些信息,而这就是通常所说的应用设置. 对于Android系统来说,系统本身的设置带来的用户体验和习惯已经深入人心,在我们的应用中同样用到类似的设置页, ...

  2. android开发之bitmap使用

    bitmap是android中重要的图像处理工具类,通过bitmap可以对图像进行剪切.旋转.缩放等操作,同时还可以指定格式和压缩质量保存图像文件. 一.拿到一个Bitmap对象 查看源码我们知道,B ...

  3. JDBC操作数据库 封装好的工具类

    mysql sqlserver oracle 数据库的驱动jar包http://download.csdn.net/download/csdn576038874/8833683package cn.h ...

  4. Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合

    项目环境背景: 操作系统:win7 JDK:1.7 相关依赖包,截图如下:

  5. Spring中 bean定义的parent属性机制的实现分析

    在XML中配置bean元素的时候,我们常常要用到parent属性,这个用起来很方便就可以让一个bean获得parent的所有属性 在spring中,这种机制是如何实现的?     对于这种情况 tra ...

  6. js 配置基础启动文件

    页面启动文件boot.js,获取存放该文件的路径,放置通用的css,js代码,方便html页面调用. __CreateJSPath = function (js) { var scripts = do ...

  7. sql Server 触发器 调用java.

    在这里,通过 xp_cmdshell 调用java程序. 步骤: 1)开启 xp_cmdshell(sql Server 2008): 通过如下命令开启: -- To allow advanced o ...

  8. SQL小细节

    平时有些小细节,不留意的话很容易得到错误的答案,我们来看下下面的代码,看看你是否能答对呢? ) ,) SELECT @str = '中国CH',@info='MyTest' SELECT [字符串]= ...

  9. acrobat GetSize 返回 x,y 值单位

    GetSize:LPDISPATCH GetSize();Description:Gets a page’s width and height in points.Parameters:Return ...

  10. iOS开发进阶-实现多线程的3种方法

    相关文章链接: 1.多线程简介 2.实现多线程的3种方法 ......待续 前言 在多线程简介中,我已经说明过了,为了提高界面的流畅度以及用户体验.我们务必要把耗时的操作放到别的线程中去执行,千万不要 ...