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. vi 快捷键

    屏幕翻滚类命令Ctrl+u:向文件首翻半屏Ctrl+d:向文件尾翻半屏Ctrl+f:向文件尾翻一屏Ctrl+b:向文件首翻一屏 shift+g:到尾部 H:跳到第一行.M:跳到中间.L:跳到最后一行. ...

  2. java 流程执行 循环 foreach循环

    一. if分支 1. 结构  if  else if   else 2.执行原则 if  if  if 结构  会一直去执行()里的判断语句 if else if  else if 结构  只要一条( ...

  3. Java基础知识强化之集合框架笔记54:Map集合之HashMap集合(HashMap<String,String>)的案例

    1. HashMap集合 HashMap集合(HashMap<String,String>)的案例 2. 代码示例: package cn.itcast_02; import java.u ...

  4. plupload使用指南(转)

    转自http://www.cnblogs.com/2050/p/3913184.html 现在随着html5技术的逐渐推广和普及,再去使用以flash为上传手段的SWFUpload显然就有点过时了,毕 ...

  5. 关于js中伪数组

    伪数组: 具有length属性: 按索引方式存储数据: 不具有数组的push().pop()等方法: 伪数组无法直接调用数组方法或期望length属性有什么特殊的行为,不具有数组的push().pop ...

  6. CSS背景颜色、背景图片、平铺、定位、固定

    CSS背景颜色设置 background-color:red;如设置背景颜色为红色: 背景颜色设置支持3种写法: 颜色名 16进制 rgb CSS背景图片颜色设置 background-image:u ...

  7. modelsim命令行仿真提示“vsim 不是内部或外部命令,也不是可运行的程序或批处理文件”的解决办法

    安装完modelsim后,用过命令行模式仿真,如“vsim -c -do run.do”,开始时是可以的. 后来偶然再用该仿真方式,发现命令行提示“vsim 不是内部或外部命令,也不是可运行的程序或批 ...

  8. 当append里面的标签显示不出来的时候,用下面的方式做

     $("#result_td").append(tem1+tem3)  $("#result_td").append($(tem1+tem3)) 

  9. C# 分页

    #region 分页 /// <summary> /// 分页 /// </summary> /// <param name="page">当前 ...

  10. dispatchkeyevent的调用机制

    http://blog.csdn.net/look85/article/details/23740761 dispatchKeyEvent和onKeyDown关系: 当键盘按下时 首先触发dispat ...