nyoj1254 Code the Tree (第七届河南省程序设计大赛)

Posted 甄情

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nyoj1254 Code the Tree (第七届河南省程序设计大赛)相关的知识,希望对你有一定的参考价值。

Code the Tree

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
描述

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
来源
第七届河南省程序设计大赛
上传者
516108736


应该是挺难的一道题了  队友敲了代码  然后就是我们三个无限的找错了

找了最少两个小时的错 

最后终于在比赛截至前4分钟 A了  有时间自己再做~

 
#include<cstdio>
#include<cstring>
#include<stack>
#include<vector>
using namespace std;
struct Node
{
	int num;
	Node* next;
	Node(){next=NULL;num=-1;}
	Node(int x){next=NULL;num=x;}
}node[55];
int du[55];
char map[100005];
stack<int>s;
vector<int>res;
int main()
{
	while(gets(map)!=NULL)
	{
		memset(du,0,sizeof(du));
		int len=strlen(map);
		int sum=0;
		for(int i=0;i<len;i++)
		{
			if(map[i]=='('&&sum)
			{
				s.push(sum);
				sum=0;
			}
			if(map[i]>='0'&&map[i]<='9')
			{
				sum=sum*10+(map[i]-'0');
			}
			if(map[i]==')')
			{
				if(sum)
				{
//					printf("sum=%d,top=%d\n",sum,s.top());
					du[sum]++;
					du[s.top()]++;
					Node* p=&node[sum];
//					printf("sum(%d)->next=\n",sum);
					while(p->next)
					{
//						printf("\tnum=%d\n",p->num);
						p=p->next;
					}
					p->next=new Node(s.top());
					
					p=&node[s.top()];
//					printf("top(%d)->next=\n",s.top());
					while(p->next)
					{
						p=p->next;
//						printf("\tnum=%d\n",p->num);
					}
					p->next=new Node(sum);
					sum=0;
				}
				else
				{
					sum=s.top();
					s.pop();
					if(!s.empty())
					{
						du[sum]++;
						du[s.top()]++;
//						printf(")sum=%d,top=%d\n",sum,s.top());
						Node* p=&node[sum];
//						printf(")sum(%d)->next=\n",sum);
						while(p->next)
						{
							p=p->next;
//							printf("\t)%d ",p->num);
//							getchar();
						}
						p->next=new Node(s.top());
						
						p=&node[s.top()];
//						printf(")top(%d)->next=\n",s.top());
						while(p->next)
						{
							p=p->next;
//							printf("\t)%d ",p->num);
//							getchar();
						}
						p->next=new Node(sum);
					}
					sum=0;
				}
			}
		}
//		Node* p=&node[5];
//		printf("5:\n");
//		while(p->next)
//		{
//			p=p->next;
//			printf("\t)%d ",p->num);
//		}
//		for(int i=0;i<9;i++)
//			printf("%d\n",du[i]);
		while(1)
		{
			int ok=0;
			for(int i=0;i<55;i++)
			{
//				printf("du[%d]=%d\n",i,du[i]);
				if(du[i]==1)
				{
//					printf("度数为1的点为%d\n",i);
					ok=1;
					res.push_back(node[i].next->num);
//					printf("要输出的点为%d\n",node[i].next->num);
					du[i]--;
					du[node[i].next->num]--;
					Node* p=&node[node[i].next->num];
					Node* q=&node[node[i].next->num];
					while(1)
					{
						p=p->next;
						if(p->num==i)
						{
							q->next=p->next;
							break;
						}
						q=p;
					}
					break;
				}
			}
			if(!ok)
				break;
		}
		for(int i=0;i<res.size();i++)
		{
			if(i==0)
				printf("%d",res[i]);
			else
				printf(" %d",res[i]);
		}
		printf("\n");
		res.resize(0);
		while(!s.empty())
			s.pop();
		memset(node,0,sizeof(node));
	}
	return 0;
}        



以上是关于nyoj1254 Code the Tree (第七届河南省程序设计大赛)的主要内容,如果未能解决你的问题,请参考以下文章

Code the Tree ZOJ - 1097

leetcode-1254

CF1254D Tree Queries(树链剖分)

nyoj221_Tree_subsequent_traversal

nyoj 484-The Famous Clock

nyoj Color the fence