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 ;
}

ural 1069. Prufer Code的更多相关文章

  1. URAL 1069 Prufer Code(模拟)

    Prufer Code Time limit: 0.25 secondMemory limit: 8 MB A tree (i.e. a connected graph without cycles) ...

  2. URAL 1069 Prufer Code 优先队列

    记录每个节点的出度,叶子节点出度为0,每删掉一个叶子,度数-1,如果一个节点的出度变成0,那么它变成新的叶子. 先把所有叶子放到优先队列中. 从左往右遍历给定序列,对于root[i],每次取出叶子中编 ...

  3. Prufer Code

    1069. Prufer Code Time limit: 0.25 secondMemory limit: 8 MB A tree (i.e. a connected graph without c ...

  4. URAL 1792. Hamming Code (枚举)

    1792. Hamming Code Time limit: 1.0 second Memory limit: 64 MB Let us consider four disks intersectin ...

  5. Ural 1780 Gray Code 乱搞暴力

    原题链接:http://acm.timus.ru/problem.aspx?space=1&num=1780 1780. Gray Code Time limit: 0.5 secondMem ...

  6. ural 1069

    题意:删除一棵树上的叶子 每删除一片叶子就写下连着该片叶子的节点  让你还原一棵树 记录每个节点连着的叶子数 0表示此时这个节点就是叶子  -1表示这个节点已经删除 删除的只能是0  就是说是叶子 暴 ...

  7. Code the Tree(图论,树)

    ZOJ Problem Set - 1097 Code the Tree Time Limit: 2 Seconds      Memory Limit: 65536 KB A tree (i.e. ...

  8. poj 2567 Code the Tree 河南第七届省赛

    Code the Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2350   Accepted: 906 Desc ...

  9. Code the Tree

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2292   Accepted: 878 Description A tree ...

随机推荐

  1. iOS 访问粘贴板

    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; pasteboard.string = self.label.text;

  2. Orleans是什么 (一)

    官网:http://dotnet.github.io/orleans/ 文档:http://dotnet.github.io/orleans/What's-new-in-Orleans 源码:http ...

  3. Mac OS

    defaults write com.apple.finder AppleShowAllFiles -bool true 此命令显示隐藏文件defaults write com.apple.finde ...

  4. Vs注释,vsXML,VSXML注释

    标签名称 说明 语法 参数 <summary> <summary> 标记应当用于描述类型或类型成员.使用<remarks> 添加针对某个类型说明的补充信息. < ...

  5. 【PHP XML与dtd概述【完整版】】

    一.XML文件简介 关于XML的大多数人都知道一些,HTML->xhtml->xml,这个顺序本来应当是HTML发展的顺序. XML可以当做配置文件使用,还可以充当小型的数据库. vers ...

  6. Code Review for SSIS package

    以下是我对SSIS包进行code review的一些建议,如果有其他更好的方案欢迎拍砖. A. 查看是否使用了最优的解决方案 1. 最优的结构视图 2. 解决方案,包,任务,组建,参数的命名使用了易读 ...

  7. 攻城狮在路上(叁)Linux(十三)--- 文件与目录的管理

    一.查看文件与目录: 1.命令格式:ls [-aAdfFhilnrRSt] 目录名称; 2.参数说明: -a:显示所有的文件,包括隐藏文件(隐藏文件一般以.开头). -d:仅列出目录本身. -l:列出 ...

  8. php开发(CI框架使用)

    年前接了一个外包项目,要求使用PHP,琢磨来琢磨去,感叹道PHP框架实在是太多了!去知乎搜索一轮,最后决定使用CI, 相关议论如下:https://www.zhihu.com/question/216 ...

  9. hdu 4069 福州赛区网络赛I DLC ***

    再遇到一个DLC就刷个专题 #include <stdio.h> #include <string.h> #include <iostream> #include ...

  10. mysql的启动

    1.直接用mysqld手工启动 [root@ora11g bin]# ./mysqld --defaults-file=../my.cnf :: [ERROR] Fatal error: Please ...