ZOJ 1097 CODE the Tree

Code the Tree

Time Limit: 2 Seconds      Memory Limit: 65536 KB

A tree (i.e. a connected graph without cycles) with vertices numbered by the integers1, 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 obtained graph, this procedure is repeated, until there is only one vertex left (which, by the way, always has numbern). 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.

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 Specification

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 that1<=n<=50.

Output Specification

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)))
(6 (1 (4)) (2 (3) (5)))

Sample Output

5 2 5 2 6 2 8
2 3
2 1 6 2 6


Source: University of Ulm Local Contest 2001

题意:根据叶子的结点,输出与叶子相邻的结点。删除叶子结点,形成新的树,从复上述操作。按顺序输出相邻的那些结点

难点:结点的存放方法

代码:

#include<iostream>
#include<vector>
#include<set>
#include<queue>
#include<cstdio>
using namespace std;

//输入树的结构
void parse(vector<set<int> >&adj,unsigned int p  = 0)
{
	unsigned int x ;//x存放的是结点的编号
	cin>>x;

	if(p)//p!=0是说明输入的不是第一个数据。从而不会造成p不存在的错误
	{
	    //两结点相邻
		adj[p].insert(x);
		adj[x].insert(p);
	}
	while(true)
	{
		char ch;
		cin>>ch;
		if(ch == ')') break;//根据输入的是'('还是‘)’来决定是否递归

		parse(adj,x);
	}
	return ;
}



int main()
{
	//freopen("input.txt","r",stdin);
	char ch;
	while(cin>>ch)                                                  //读'(',同是也便 于结束
	{
		vector <set<int> > adj(1024,set<int>());                   //1024 个结点,初始值为0
		parse(adj);                                               //输入结点



		priority_queue<int , vector<int>,greater<int> >leafs;//优先队列,用来存放叶子结点
		int n = 0;//结点个数
		for(unsigned int i = 0;i<adj.size();i++)
		{
			if(adj[i].size())
			{
				n++;
				if(adj[i].size()==1)//只有一个相邻结点
				{
					leafs.push(i);
				}
			}
		}

		for(int k = 1;k<n;k++)
		{
			unsigned int x = leafs.top();
			leafs.pop();

			unsigned int p = *(adj[x].begin());
			if(k>1) cout<<" ";
			cout<<p;
			adj[p].erase(x);

			if(adj[p].size()==1)
				leafs.push(p);
		}
		cout<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值