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;
}        
时间: 2024-07-30 13:50:46

nyoj1254 Code the Tree (第七届河南省程序设计大赛)的相关文章

nyoj1253 Turing equation(第七届河南省程序设计大赛)

题目1253 题目信息 运行结果 本题排行 讨论区 Turing equation 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述 The fight goes on, whether to store  numbers starting with their most significant digit or their least  significant digit. Sometimes  this  is also called  the  "Endian

nyoj1248 海岛争霸(第七届河南省程序设计大赛)

题目1248 题目信息 运行结果 本题排行 讨论区 海岛争霸 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比海盗,你知道吧?杰克船长驾驶着自己的的战船黑珍珠1号要征服各个海岛的海盜,最后成为海盗王. 这是一个由海洋.岛屿和海盗组成的危险世界.杰克船长准备从自己所占领的岛屿A开始征程,逐个去占领每一个岛屿.面对危险重重的海洋与诡谲的对手,如何凭借智慧与运气,建立起一个强大的海盗帝国. 杰克船长

nyoj1255 Rectangles(第七届河南省程序设计大赛)

题目1255 题目信息 运行结果 本题排行 讨论区 Rectangles 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 Given N (4 <= N <= 100)  rectangles and the lengths of their sides ( integers in the range 1..1,000), write a program that finds the maximum K for which there is a sequence

nyoj1249 物资调度 (第七届河南省程序设计大赛)

物资调度 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 某地区发生了地震,灾区已经非常困难,灾民急需一些帐篷.衣物.食品和血浆等物资.可通往灾区的道路到处都是塌方,70%以上的路面损坏,桥梁全部被毁.国家立即启动应急预案,展开史上最大强度非作战空运行动,准备向灾区空投急需物资. 一方有难,八方支援.现在已知有N个地方分别有A1,A2,-.,An个物资可供调配.目前灾区需要物资数量为M. 现在,请你帮忙算一算,总共有多少种物质调度方案. 假设某地方一旦被选择调配,则

河南省第七届ACM程序设计大赛赛后总结

我们学校ACM集训开始于4月5号(清明节),5月25日在郑州解放军信息工程大学举办,集训耗时50天,但是感觉效果还行,但是也不是太好:我们也已经尽力了,虽然说只拿了个银牌,每份收获的背后,都会有辛勤的汗水,毕竟我们也努力了: 下面说说比赛中的问题:觉得吧,虽说模拟赛参加了很多,但是到了正式的比赛的时候,还是出现了紧张的情况,九点开始比赛,我们AC第一道题的时间是在十点半左右,题目不难而且曾经也做过啊,但是就是调试不好,越调试不好,心里就越急啊:刚开始看题的时间,我们都看了第一道题,意思也都明白,

nyoj714 Card Trick(第六届河南省程序设计大赛)

题目714 题目信息 运行结果 本题排行 讨论区 Card Trick 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 The magician shuffles a small pack of cards, holds it face down and performs the following procedure: The top card is moved to the bottom of the pack. The new top card is deal

nyoj 711最舒适的路线(第六届河南省程序设计大赛 并查集)

最舒适的路线 时间限制:5000 ms  |  内存限制:65535 KB 难度:5 描述 异形卵潜伏在某区域的一个神经网络中.其网络共有N个神经元(编号为1,2,3,-,N),这些神经元由M条通道连接着.两个神经元之间可能有多条通道.异形卵可以在这些通道上来回游动,但在神经网络中任一条通道的游动速度必须是一定的.当然异形卵不希望从一条通道游动到另一条通道速度变化太大,否则它会很不舒服. 现在异形卵聚居在神经元S点,想游动到神经元T点.它希望选择一条游动过程中通道最大速度与最小速度比尽可能小的路

NYOJ - 716 - River Crossing --第六届河南省程序设计大赛 (简单DP!!)

River Crossing 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 Afandi is herding N sheep across the expanses of grassland  when he finds himself blocked by a river. A single raft is available for transportation. Afandi knows that he must ride on the raft for

NYOJ - 715 - Adjacent Bit Counts --第六届河南省程序设计大赛 (DP!!)

Adjacent Bit Counts 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 For a string of n bits x1, x2, x3, -, xn,  the adjacent bit count of the string  is given by     fun(x) = x1*x2 + x2*x3 + x3*x 4 + - + xn-1*x n which counts the number of times a 1 bit is adj