您的位置:首页 > Web前端

poj 2567 Code the Tree 【还原树 求prufer序列】

2016-03-04 17:21 344 查看
Code the Tree

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2401 Accepted: 923
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 obtained graph, 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)))
(6 (1 (4)) (2 (3) (5)))

Sample Output
5 2 5 2 6 2 8
2 3
2 1 6 2 6


题意:给你一个字符串表示一棵无根树。让你求出该树的purfer序列。

思路:递归建立树,然后求purfer序列。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
const int MOD = 1e9+7;
const int MAXN = 1000+10;
const int MAXM = 2*1e6+10;
const int INF = 0x3f3f3f3f;
void add(LL &x, LL y) {x += y; x %= MOD;}
char str[MAXN];
int Map[60][60];
int n;
int getval(int L)
{
int num = 0;
for(int i = L+1; ; i++)
{
if(str[i] >= '0' && str[i] <= '9')
num = num * 10 + (str[i] - '0');
else {
break;
}
}
n = max(n, num);
return num;
}
int in[60];
void getMap(int L, int R)
{
if(R - L <= 3) return ;
int root = getval(L);
int cnt = 0; int pos;
for(int i = L+1; i < R; i++)
{
if(str[i] == '(')
{
cnt++;
if(cnt == 1)
{
pos = i; int val = getval(i);
Map[root][val] = Map[val][root] = 1;
in[root]++; in[val]++;
}
}
else if(str[i] == ')')
{
cnt--;
if(cnt == 0)
getMap(pos, i);
}
}
}
int Find()
{
int leaf = 0, node = 0;
for(int i = 1; i <= n; i++)
{
if(in[i] == 1)
{
leaf = i;
for(int j = 1; j <= n; j++)
{
if(Map[i][j])
{
node = j;
break;
}
}
}
if(node) break;
}
in[leaf]--; in[node]--;
Map[node][leaf] = Map[leaf][node] = 0;
return node;
}
int main()
{
while(gets(str))
{
int len = strlen(str);
CLR(Map, 0); n = 0;
getMap(0, len-1);
queue<int> Q;
for(int i = 1; i <= n-1; i++){
Q.push(Find());
}
int cnt = 0;
while(!Q.empty()) {
if(cnt) printf(" ");
printf("%d", Q.front()); Q.pop();
cnt++;
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: