G Code the Tree

G Code the Tree


  • 内存限制:64MB 时间限制:1s Special Judge: No


题目描述:

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.Sample Input

样例输入:

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

样例输出:

5 2 5 2 6 2 8
2 3

题目翻译过来就是:寻找那些度为1&&编号最小的结点,输出他的相邻结点的编号。

解题思路就是运用栈,将所给数字转化为图,然后利用类似拓扑排序的思想输出所有的结点

由于题中说明最多的节点数目为50个所以为了方便,用邻接矩阵存储图即可

#include<iostream>
#include<cstring>
#include<vector>
#include<stack>
#include<cstdio>
using namespace std;

int Graph[55][55];
int visite[55];//标记结点是否被访问 
int cnt[55];//记录结点的 度 

int main()
{
	char s[1000];
	while(gets(s)!=NULL)//gets()函数在cstdio中 
	{
		memset(Graph,0,sizeof(Graph));
		memset(cnt,0,sizeof(cnt));
		int len=strlen(s);
		int i,j;
		stack<int> sta;//栈中用于存储结点编号 
		int max_number=0;//记录当前最多的结点个数 
		for(i=0;i<len;i++)
		{
			if(s[i]=='(')// ( 后面一定是数字  
			{
				int sum=0;
				i++;
				while(s[i]>='0'&&s[i]<='9')
				{
					sum=sum*10+s[i]-'0';
					i++;
					if(sum>max_number)
						max_number=sum;
				}	
				if(!sta.empty())
				{
					int a=sta.top();
					Graph[a][sum]=1;
					Graph[sum][a]=1;
					cnt[a]++;
					cnt[sum]++;
				}				
				sta.push(sum);
			}
			if(s[i]==')')
				sta.pop();	
				
		}
		memset(visite,0,sizeof(visite));
		bool flag=true;
		int cun[55];
		int number=0;
		while(flag)
		{
			flag=false;
			for(i=1;i<max_number;i++)
			{
				if(visite[i]==0&&cnt[i]==1)
				{
					visite[i]=1;
					for(j=1;j<=max_number;j++)
						if(Graph[i][j]==1)
						{
							Graph[j][i]=0;
							cnt[j]--;
							cun[number++]=j;
							break;
						}
					
					flag=true;
					break;
				}
			}
		}
		for(i=0;i<number-1;i++)
			cout<<cun[i]<<" ";
		cout<<cun[number-1]<<endl;
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/lb_78596935/article/details/80427268
今日推荐