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. RFC 文档(中文与英文)

    http://man.chinaunix.net/develop/rfc/default.htm https://www.rfc-editor.org/retrieve/ http://www.iet ...

  2. Linux查看当前系统登录用户、登录日志、登录错误日志

    1.查看当前系统的登录用户 w who 2.查看成功登录历史记录 last -n 3.查看尝试登录失败的历史记录 lastb -n 4.显示每个用户最近一次登录成功的信息 lastlog

  3. HTML+CSS基础学习笔记(1)

    一.了解HTML.CSS.JS 1.HTML是网页内容的载体. 内容就是网页制作者放在页面上想要让用户浏览的信息,可以包含文字.图片.视频等. 2.CSS样式是表现. 用来改变内容外观的东西称之为表现 ...

  4. (转)Asp.Net 请求处理机制

    原文:http://www.cnblogs.com/cilence/archive/2012/05/28/2520712.html Asp.Net 请求处理机制   前言 我们都知道Web请求响应是基 ...

  5. EventBus3 简单使用及注意点

    博客: 安卓之家 微博: 追风917 CSDN: 蒋朋的家 简书: 追风917 # EventBus3 简介 EventBus Android 发布/订阅事件总线,可简化 Activities, Fr ...

  6. 源码心德`Context`类

    Context,中文直译为“上下文”,SDK中对其说明如下: Interface to global information about an application environment. Thi ...

  7. servlet(一)

    百度百科是这样的: Servlet 是在服务器上运行的小程序.这个词是在 Java applet 的环境中创造的.虽然后者已很少被使用,但 Servlet 却发展的很好.是一般面试都会常考的知识. 维 ...

  8. oracle学习笔记2:创建修改表

    1.创建表 CREATE TABLE ORDERINFO ( ORDERID NUMBER(*, 0) NOT NULL , ORDERCODE VARCHAR2(20 BYTE) NOT NULL ...

  9. Java的jLinqer包介绍

    1.介绍 熟悉C#的lambda 表达式,想要使用 LINQ 在 Java,是个问题,虽然 Java8用函数式接口已经实现了部分lambda 表达式,但是还是不够灵活. 2.Linqer简介 Linq ...

  10. 关于vs2008使用oracleclient链接oracle数据库报报错OCIEnvCreate 失败,返回代码为 -1,但错误消息文本不可用

    用vs2008链接oracle数据库出现问题,报错OCIEnvCreate 失败,返回代码为 -1,但错误消息文本不可用,从网上找了好久方法,有两种oracle客户端文件权限,和运行vs2008以管理 ...