ural 1069. Prufer Code


1069. Prufer Code

Time limit: 0.25 second
Memory limit: 8 MB
A tree (i.e. a connected graph without cycles) with vertices is given ( N ≥ 2). Vertices of the tree are numbered by the integers 1,…, N. A Prufer code for the tree is built as follows: a leaf (a vertex that is incident to the only edge) with a minimal number is taken. Then this vertex and the incident edge are removed from the graph, and the number of the vertex that was adjacent to the leaf is written down. In the obtained graph once again a leaf with a minimal number is taken, removed and this procedure is repeated until the only vertex is left. It is clear that the only vertex left is the vertex with the number  N. The written down set of integers ( N−1 numbers, each in a range from 1 to  N) is called  a Prufer code of the graph.
Your task is, given a Prufer code, to reconstruct a tree, i.e. to find out the adjacency lists for every vertex in the graph.
You may assume that 2 ≤  N ≤ 7500

Input

A set of numbers corresponding to a Prufer code of some tree. The numbers are separated with a spaces and/or line breaks.

Output

Adjacency lists for each vertex. Format: a vertex number, colon, numbers of adjacent vertices separated with a space. The vertices inside lists and lists itself should be sorted by vertex number in an ascending order (look at sample output).

Sample

input output
2 1 6 2 6
1: 4 6
2: 3 5 6
3: 2
4: 1
5: 2
6: 1 2
Problem Author: Magaz Asanov
Problem Source: Ural State Univerisity Personal Contest Online February'2001 Students Session
给一组序列进行树的重构;

对着序列看了好久,正着模拟,反着又模拟,就是出不来;后来发现正着序列看时,每次的叶子节点都是那么几个。也就是出现在原序列中的肯定刚开始都不是叶子,比如样例刚开始就只有3,4,5三个叶子。由于每次都是选最小的数的叶子剪掉,所以重组的时候也要拿最小的叶子,这里可以用set来维护。对于每次加上树叶后的节点,比如节点2,加了3之后,他的度就会减少,当度减到0的时候,此时2这课树没有扩展的节点了,此时他就成了树叶,加入到树叶set里。如此反复,可以得到整棵树。

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
#define N 8000
#define M 1000005
#define INF 1000000007
#define LINF 1000000000000001LL
#define LL long long
#define ULL unsigned long long
#define UINT unsigned int
#define clr(x,v); memset(x,v,sizeof(x));
int pc[N];
int n;
set<int>leaf;
set<int>edge[N];
set<int>::iterator it;
int deg[N];
int main()
{
    n=0;
    int i;
    clr(deg,0);
    while(~scanf("%d",&pc[n++]))
        deg[pc[n-1]]++;
    --n;
    for(int i=1; i<=n; i++)
        if(deg[i]==0) leaf.insert(i);
    int u,v;
    for(int i=1; i<=n; i++) edge[i].clear();
    for(int i=0; i<n; i++)
    {
        it=leaf.begin();
        u=pc[i];
        v=*it;
        edge[u].insert(v);
        edge[v].insert(u);
        deg[u]--;
        leaf.erase(it);
        if(deg[u]==0) leaf.insert(u);
    }
    for(i=1; i<=n+1; i++)
    {
        printf("%d:",i);
        for(it=edge[i].begin(); it!=edge[i].end(); it++)
            printf(" %d",*it);
        puts("");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值