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. iOS - CADisplayLink与NSTimer

    一.CADisplayLink简介 CADisplayLink 是一个定时器对象可以让你的应用以与显示器的刷新界面相同的频率进行绘图. 应用如果想要创建 display link ,需要提供一个目标对 ...

  2. 记录一些容易忘记的属性 -- UIGestureRecognize手势

    //一个手势只能添加到一个view上面 //设置当前手势需要的点击次数    _tapRec.numberOfTapsRequired = 1;//(默认为1)    //设置当前需要几个手指同时点击 ...

  3. jQuery 动态添加瀑布流

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. JDK的下载与安装

    一.下载 在Oracle公司的官方网站(www.oracle.com)下载. 二.安装 1.双击运行JDK程序,弹出JDK安装导向窗口,点击“下一步” 2.点击“更改",将安装地址修改为 C ...

  5. CentOS中配置LNMP环境打开提示File not found

    在centos系统中配置好php环境了,但是发现能运行html页面并不能运行php文件了,这样我就在gg的帮助下一步不解决了,下面来看问题的具体解决过程.     安装之后测试发现,怎么Html能运行 ...

  6. SVG 2D入门5 - 颜色的表示

    SVG和canvas中是一样的,都是使用标准的HTML/CSS中的颜色表示方法,这些颜色都可以用于fill和stroke属性.基本有下面这些定义颜色的方式:1. 颜色名字: 直接使用颜色名字red, ...

  7. hdu 2035

    Ps:查了下快速幂,顺便在这用下.... 积的求余等于两个数的求余的积再求余... 代码: #include "stdio.h"int mod(int a,int b);int m ...

  8. Spring MVC配置文件解释

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  9. 过期邮件替换SQL

  10. select2美化下拉单

    http://www.51xuediannao.com/js/jquery/select2.html http://www.51xuediannao.com/demo.php