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
【题意】给你一个树(无向),每次去掉一个入度为零的编号最小的点,然后记录与这个点相邻的那个点,一直操作下去知道只剩下一个点。然后输入记录的点序列,输出那棵树。
【分析】输入数据中未出现的点肯定是入度为0的,将他们全部放入优先队列(从小到大),然后依次取出输入数据和优先队列中的点,将其连边,并将输入数据的入度-1,若==1,
 放入优先队列。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 7e3+;
const int M = +;
const int mod=1e9+;
int n=,m,k,tot=,s,t;
int head[N],vis[N],in[N],sum[N];
struct cmp{bool operator () (int &a,int &b){return a>b;} };
int main()
{
priority_queue<int,vector<int>,cmp>q;
vector<int>vec,edg[N];int cnt=;
for(int i=;i<N;i++)in[i]=;
while(~scanf("%d",&k)){
vec.pb(k);in[k]++;n=max(n,k);//cnt++;if(cnt==7)break;
}
in[n]--;
for(int i=;i<=n;i++){
if(in[i]==){
q.push(i);
}
}
for(int i=;i<vec.size();i++){
int u=vec[i];
int v=q.top();q.pop();
edg[u].pb(v);edg[v].pb(u);
in[u]--;
if(in[u]==)q.push(u);
}
for(int i=;i<=n;i++){
printf("%d:",i);
sort(edg[i].begin(),edg[i].end());
for(int j=;j<edg[i].size();j++){
printf(" %d",edg[i][j]);
}printf("\n");
}
return ;
}

URAL 1069 Prufer Code(模拟)的更多相关文章

  1. ural 1069. Prufer Code

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

  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. [fun code - 模拟]孤独的“7”

    今天看到朋友圈里有人发了一张孤独的7的题目,第一反应就是模拟后计算出结果,而女朋友则更爱推理,手算.

  6. URAL 1944 大水题模拟

    D - Record of the Attack at the Orbit Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format ...

  7. UVA 1593: Alignment of Code(模拟 Grade D)

    题意: 格式化代码.每个单词对齐,至少隔开一个空格. 思路: 模拟.求出每个单词最大长度,然后按行输出. 代码: #include <cstdio> #include <cstdli ...

  8. Ural 1780 Gray Code 乱搞暴力

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

  9. URAL 2073 Log Files (模拟)

    题意:给定 n 场比赛让你把名称,时间,比赛情况按要求输出. 析:很简单么,按照要求输出就好,注意如果曾经AC的题再交错了,结果也是AC的. 代码如下: #pragma comment(linker, ...

随机推荐

  1. 百度地图开发 android App 数字签名(SHA1)获取办法

    简述: a.输入keytool -list -v -keystore debug.keystore,会得到三种指纹证书,选取SHA1类型的证书(密钥口令是android),这个获取到的SHA1的值和e ...

  2. ODI中通过配置表和自定义逆向工程获取数据库信息

    自定义逆向工程RKM 从配置表meta_db, meta_table, meta_column, meta_key中获取生产库的元数据信息.

  3. Node.js的简介和安装

    一.Node.js的简介和安装 a)       什么是Node.js? Node.js是一个开发平台 让JavaScript运行在服务器端的开发平台 ---简单点说就是用JavaScript写服务器 ...

  4. 关于GSMMAP分支cell_log扫描不正常问题的解决办法

    阔别多年,本周在KALI 2.0下重拾旧时趣味,可怎么折腾都未曾见ARFCN,迫不得已还刷了brust_ind分支 才达到目的.后经仔细翻阅官方文档发现此问题早有披露,解决方案也已经公布,逐分享给大家 ...

  5. php大力力 [001节]2015-08-21.php在百度文库的几个基础教程新手上路日记 大力力php 大力同学 2015-08-21 15:28

    php大力力 [001节]2015-08-21.php在百度文库的几个基础教程新手上路日记 大力力php 大力同学 2015-08-21 15:28 话说,嗯嗯,就是我自己说,做事认真要用表格,学习技 ...

  6. Qemu文档

    http://wiki.qemu.org/Manual http://qemu.weilnetz.de/qemu-doc.html http://www.linuxcertif.com/man/1/q ...

  7. Java容器类List,ArrayList及LinkedList

    List容器类图 List是一个接口,它继承自Collection和Iterable,它的实现类有AbstractList,AbstrackSequenceList,ArrayList,LinkedL ...

  8. python3爬虫初探(一)之urllib.request

    ---恢复内容开始--- #小白一个,在此写下自己的python爬虫初步的知识.如有错误,希望谅解并指出. #欢迎和大家交流python爬虫相关的问题 #2016/6/18 #----第一把武器--- ...

  9. HDOJ-三部曲一(搜索、数学)-1005-Dungeon Master

    Dungeon Master Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tot ...

  10. APNS IOS PHP 苹果推送

    IOS---APNS 消息推送实践 首先,需要一个pem的证书,该证书需要与开发时签名用的一致. 具体生成pem证书方法如下: 1. 登录到 iPhone Developer Connection P ...