题目链接:http://nyoj.top/problem/1254

  • 内存限制:64MB 时间限制:1000ms

题目描述:

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 new obtained tree, 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".

输入描述

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

输出描述

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.

样例输入

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

样例输出

5 2 5 2 6 2 8
2 3

解题思路

题意:给你一个字符串表示一棵无根树。让你求出该树的purfer序列。
思路:模拟题。根据给的串建树,左括号进入下一层,右括号返回上一层。建树完成后,维护每个点的度,依次删除度为1的最小的点和它唯一的边,输出边连接的另一个点。注意树根也可以作为叶子,只要点的度为1,它就是叶子。

#include <bits/stdc++.h>
using namespace std;
int p, n, deg[55], mp[55][55];
int Create(char str[]) {
    int num, wei, ans;
    while (str[p]) {
        if (isdigit(str[p])) {
            sscanf(str + p, "%d%n", &num, &wei);
            p += wei;
            n++;
        }
        else if (str[p] == '(') {
            p++;
            ans = Create(str);
            deg[num]++;
            deg[ans]++;
            mp[ans][num] = mp[num][ans] = 1;
        }
        else if (str[p] == ')') {
            p++;
            return num;
        }
        else p++;
    }
}
int main() {
    char str[250];
    while (~scanf("%[^\n]", str)) {
        getchar();
        n = 0, p = 1;
        memset(mp, 0, sizeof(mp));
        memset(deg, 0, sizeof(deg));
        Create(str);
        for (int i = 0; i < n - 2; i++) {
            for (int j = 1; j < n; j++) {
                if (deg[j] == 1) {
                    deg[j] = 0;
                    for (int k = 1; k <= n; k++) {
                        if (mp[j][k]) {
                            deg[k]--;
                            printf("%d ", k);
                            mp[j][k] = mp[k][j] = 0;
                            break;
                        }
                    }
                    break;
                }
            }
        }
        printf("%d\n", n);
    }
    return 0;
}