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

Tags: graph theory  (hide tags for unsolved problems)
Difficulty: 522
题意:给出prufer code,要求求出原树。
分析:知道prufercode之后就很简单了。

 1 /**
 2 Create By yzx - stupidboy
 3 */
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <cstdlib>
 7 #include <cmath>
 8 #include <deque>
 9 #include <vector>
10 #include <queue>
11 #include <iostream>
12 #include <algorithm>
13 #include <map>
14 #include <set>
15 #include <ctime>
16 #include <iomanip>
17 using namespace std;
18 typedef long long LL;
19 typedef double DB;
20 #define MIT (2147483647)
21 #define INF (1000000001)
22 #define MLL (1000000000000000001LL)
23 #define sz(x) ((int) (x).size())
24 #define clr(x, y) memset(x, y, sizeof(x))
25 #define puf push_front
26 #define pub push_back
27 #define pof pop_front
28 #define pob pop_back
29 #define ft first
30 #define sd second
31 #define mk make_pair
32
33 inline int Getint()
34 {
35     int Ret = 0;
36     char Ch = ' ';
37     bool Flag = 0;
38     while(!(Ch >= '0' && Ch <= '9'))
39     {
40         if(Ch == '-') Flag ^= 1;
41         Ch = getchar();
42     }
43     while(Ch >= '0' && Ch <= '9')
44     {
45         Ret = Ret * 10 + Ch - '0';
46         Ch = getchar();
47     }
48     return Flag ? -Ret : Ret;
49 }
50
51 const int N = 7510;
52 int n, arr[N];
53 int cnt[N];
54 priority_queue<int> que;
55 vector<int> ans[N];
56
57 inline void Input()
58 {
59     int x;
60     while(cin >> x)
61     {
62         arr[n++] = x;
63         cnt[x]++;
64     }
65     n++;
66 }
67
68 inline void Solve()
69 {
70     for(int i = 0; i < n; i++)
71         if(!cnt[i + 1]) que.push(-(i + 1));
72     for(int step = 1; step <= n - 1; step++)
73     {
74         int x = -que.top(), now = arr[step - 1];
75         que.pop();
76         if(!(--cnt[now])) que.push(-now);
77         ans[now].pub(x);
78         ans[x].pub(now);
79     }
80
81     for(int i = 1; i <= n; i++)
82     {
83         printf("%d:", i);
84         sort(ans[i].begin(), ans[i].end());
85         int length = sz(ans[i]);
86         for(int j = 0; j < length; j++)
87             printf(" %d", ans[i][j]);
88         printf("\n");
89     }
90 }
91
92 int main()
93 {
94     freopen("a.in", "r", stdin);
95     Input();
96     Solve();
97     return 0;
98 }

View Code

转载于:https://www.cnblogs.com/StupidBoy/p/5076812.html

ural 1069. Prufer Code相关推荐

  1. Ural 1780 Gray Code 乱搞暴力

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

  2. pku,zju题目分类

    哎呦喂,直接ctrl+A了.话说浙江大学的题还见过的呢.. 公告: [意见反馈][官方博客]   ural pku Zju 题目分类 收藏   感谢 mugu 的提供.... Ural Problem ...

  3. 《题目与解读》红书 训练笔记目录《ACM国际大学生程序设计竞赛题目与解读》

    虽然2012年出版的老书了,但是是由三次世界冠军的上海交大ACM队出版的书籍,选择的题目是ACM经典中的经典,书中有非常详细的题解,可以学到很多东西,值得一刷. 目录 第一部分 第一章 数学 1.1 ...

  4. 第三篇 层次类非线性表的编程实验 第10章 应用经典二叉树编程

    10.1 二叉搜索树的实验范例        显然,二叉搜索树的中序遍历为递增序列.        10.1.1 BST(Binary Search Tree)        10.1.2 Falli ...

  5. 解题报告(八) prufer 序列与 Cayley 公式(ACM / OI)超高质量题解

    繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量题解和代码,题目难度不一定按照题号排序,我会在每道题后面加上题目难度指数(1∼51 \sim 51∼5),以模板题难度 11 ...

  6. PAT 1069 1070 1071 1072

    pat 1069 The Black Hole of Numbers 水题,代码如下: 1 #include<cstdio> 2 #include<cstdlib> 3 #in ...

  7. hdu 1069 Monkey and Banana (LIS)

    Problem - 1069 随便找到的一道题目. 题意是给出一些的长方体,长方体可以用任意次数,可以任意翻转.如果一个长方体可以叠在另一个长方体上,条件是这个长方体的长和宽严格小于另一个长方体的长和 ...

  8. [代码]ural 1655 Somali Pirates

    Abstract ural 1655 Somali Pirates dp Source http://acm.timus.ru/problem.aspx?space=1&num=1655 So ...

  9. Code::Blocks 的配色方案

    codeblocks的配置文件是default.conf, 在Windows系统下,该文件在C:\Documents and Settings\Administrator\Application Da ...

  10. ural 1155. Troubleduons

    1155. Troubleduons Time limit: 0.5 second Memory limit: 64 MB Archangel of the Science is reporting: ...

最新文章

  1. linux如何安装VM虚拟机
  2. UI自动化新思路-基于RUNTIME的自动化测试设想
  3. CCS中如何新建Platform以及调用
  4. 017.Zabbix宏介绍
  5. SendKeys中特殊字符的键代码
  6. MySQL+Amoeba实现数据库主从复制和读写分离
  7. AI「抄」代码无罪?GitHub Copilot拿用户的开源代码改一改就去挣钱!
  8. 腾讯广告算法大赛 | 复赛第一周周冠军心得分享
  9. web前端网站推荐(后续继续补充)
  10. chrome强制使用HSTS原理
  11. 如何刷新linux的fdisk,②linux fdisk
  12. 知识图谱 helloword
  13. threejs 效果合成器(EffectComposer)
  14. 【干货分享】硬件测试工程师必备基本技能,看这一篇就够!
  15. 计算机未来新CPU,桌面CPU性能排行 CPU天梯图2017年11月最新版 (全文)
  16. 当程序员遇到有远见的丈母娘,找对象那不是事
  17. Scratch入门教程:第二节 简单的交互
  18. SAP中计量单位有关的表
  19. 深入理解DOM事件类型系列第三篇——变动事件
  20. WIN10 动态磁盘转基本磁盘

热门文章

  1. Python(2)模块和数据类型
  2. Ubuntu桌面美化教程
  3. 微表情如何用计算机分析计算,基于差分定位与光流特征提取的微表情识别 - 计算机应用与软件.pdf...
  4. 如何查找计算机密码cmd,教你如何查看计算机所连wifi密码
  5. [学习]17 每天只睡6小时,依然精力充沛
  6. 自棱镜事件,隐私保护搜索引擎DuckDuckGo流量增长600%
  7. 机器学习笔记week1——奥卡姆剃刀原则、L1与L2范数正则化、模型泛化性
  8. 透过 AI 技术解读人的行为 研究开发回声定位
  9. DAS NAS SAM FC
  10. php 批量下载网页文件,批量下载文件(以xxx网站为例)