21435_DecodetheTree

2022-5-16 18:19| 发布者: Hocassian| 查看: 41| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-4-89505261068900-Problem List-采集的数据-后羿采集器.html

Pro.ID

21435

Title

Decode the Tree

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=21435

AC

0

Submit

0

Ratio

-

时间&空间限制

  • Time Limit: 1000/500 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others)
  • 描述

    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, to reconstruct a tree, given its Prufer code. The tree should be 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 output. To generate further sample input, you may use your solution to Problem 2567.
    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".

    输入

    The input contains several test cases. Each test case specifies the Prufer code of a tree on one line. You will find n-1 numbers separated by a single space. Input is terminated by EOF. You may assume that 1 ≤ n 50.

    输出

    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, to reconstruct a tree, given its Prufer code. The tree should be 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 output. To generate further sample input, you may use your solution to Problem 2567.
    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 the Prufer code of a tree on one line. You will find n-1 numbers separated by a single space. Input is terminated by EOF. You may assume that 1 ≤ n 50.

    Output

    For each test case generate a single line containing the corresponding tree, denoted as described above. Note that, in general, there are many ways to denote such a tree: choose your favourite one.

    Sample Input
    5 2 5 2 6 2 8
    2 3
    2 1 6 2 6
    Sample Output

    (8 (2 (3) (5 (1) (4)) (6 (7))))
    (3 (2 (1)))
    (6 (1 (4)) (2 (3) (5)))

    Source

    样例输入

    5 2 5 2 6 2 8
    2 3
    2 1 6 2 6

    样例输出

    (8 (2 (3) (5 (1) (4)) (6 (7))))
    (3 (2 (1)))
    (6 (1 (4)) (2 (3) (5)))

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部