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
Difficulty: 522
 
题意:给出prufer code,要求求出原树。
分析:知道prufercode之后就很简单了。
 /**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = ;
int n, arr[N];
int cnt[N];
priority_queue<int> que;
vector<int> ans[N]; inline void Input()
{
int x;
while(cin >> x)
{
arr[n++] = x;
cnt[x]++;
}
n++;
} inline void Solve()
{
for(int i = ; i < n; i++)
if(!cnt[i + ]) que.push(-(i + ));
for(int step = ; step <= n - ; step++)
{
int x = -que.top(), now = arr[step - ];
que.pop();
if(!(--cnt[now])) que.push(-now);
ans[now].pub(x);
ans[x].pub(now);
} for(int i = ; i <= n; i++)
{
printf("%d:", i);
sort(ans[i].begin(), ans[i].end());
int length = sz(ans[i]);
for(int j = ; j < length; j++)
printf(" %d", ans[i][j]);
printf("\n");
}
} int main()
{
freopen("a.in", "r", stdin);
Input();
Solve();
return ;
}

最新文章

  1. ListView的LayoutParams设置
  2. 如何用pdb进行python调试
  3. NodeJS package.json
  4. Fiddler进行手机抓包
  5. ci下面的增删改查
  6. python在linux上的GUI无法弹出界面
  7. Spring概述--1
  8. php的ftp类
  9. 我的Python成长之路---第七天---Python基础(21)---2016年2月27日(晴)
  10. C++中的继承(3)作用域与重定义,赋值兼容规则
  11. 你不知道的 #include
  12. Redis的数据结构之字符串
  13. Shell egrep
  14. A1008. Elevator
  15. 使用JSONP实现跨域通信
  16. javascript 高级程序设计 十一
  17. node-webkit学习(2)基本结构和配置
  18. matlab中norm函数的用法
  19. php 打印
  20. Hive shell 命令

热门文章

  1. 高效使用你的Xcode
  2. iOS 访问粘贴板
  3. mysql扩展库-1
  4. MVC - 20.前台ajax分页
  5. Linux常用的日志分析命令与工具
  6. 以 MAMP 为 Mac OS X 安装并设置 PHP开发环境
  7. PowerDesigner生成sql及HTML格式数据库文档
  8. 在 Mac 上安装 sbt
  9. Golang gopath
  10. 静态内容生成器&mdash;&mdash;Wyam