G.Code the Tree

来源:互联网 发布:网络上豆汁是什么意思 编辑:程序博客网 时间:2024/06/05 17:56

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 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".

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)))

Sample Output

5 2 5 2 6 2 8
2 3

主要是题意,了解题意就很简单,给出一组广义表,得到图形表示,每次去掉只有一条边相连的点(值最小的那一个)输出和它相连的那个点并将这条边去掉
与2相连的最开始有6,3,5,4,8
与6相连有2,7
与3相连只有2
与5相连有1,2,4
与1相连只有5,
与4相连只有5,
与7相连只有6
与8相连只有2
所以最后答案 5 2 5 2 6 2 8
#include<bits/stdc++.h>using namespace std;int main(){    char st[10000];    while(gets(st)!=NULL)    {        stack<int>qi;        vector<int>a[55];        int ma=-1;        for(int i=0;i<strlen(st);i++)        {            if(st[i]=='(')//判断是否是边的其中一端            {                int s=0;                i++;                while(st[i]>='0'&&st[i]<='9')                {                    s=s*10+st[i]-'0';                    i++;                }                ma=max(ma,s);//确定长度                if(!qi.empty())//判断是否已经有一个端点                {                    int l=qi.top();                    a[l].push_back(s);                    a[s].push_back(l);                }                qi.push(s);端点入栈                i--;            }            if(st[i]==')')//该端点的所有边结束出栈                qi.pop();        }        int b[55];        int t=0;        for(int i=1;i<ma;i++)        {            for(int j=1;j<=ma;j++)            {                if(a[j].size()==1)                {                    int sh=a[j][0];                    b[t]=a[j][0];                    t++;                    for(int k=0;k<a[sh].size();k++)                        if(a[sh][k]==j)//取端点,去边                        {                            int l=a[sh][k];                           a[sh][k]=a[sh][a[sh].size()-1];                           a[sh][a[sh].size()-1]=l;                           a[sh].pop_back();                        }                    a[j].pop_back();                    break;                }            }        }        printf("%d",b[0]);        for(int i=1;i<t;i++)        {            printf(" %d",b[i]);        }        printf("\n");    } }

0 0
原创粉丝点击