Poj code pufer Number of the tree

Source: Internet
Author: User
Code the tree
Time limit:1000 ms   Memory limit:65536 K
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 equals (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 abve, the root of a tree may be a leaf as well. it is only for the permission 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 abve 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 82 32 1 6 2 6

Source

Ulm Local 2001 should be a question of water, so even Edge tables are not open, the set is messed up, and then it takes an hour. Note the format of the entire row read: cin. custom comparison function of Getline (STR, maxl, '\ n') priority_queue: priority_queue <int, vector <int>, cmp_c> q; Sequence output stored in common arrays, when the sequence is null, it must be judged.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.