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. POJ 1777 mason素数

    题目大意: 给定数列 a1 , a2 , ... , an 希望找到一个  N = sigma(ai^ki)  , (0<=ki<10) ,ki可随自己定为什么 只要保证N的因子和可以表示 ...

  2. HDU 1098 Ignatius's puzzle 费马小定理+扩展欧几里德算法

    题目大意: 给定k,找到一个满足的a使任意的x都满足 f(x)=5*x^13+13*x^5+k*a*x 被65整除 推证: f(x) = (5*x^12 + 13 * x^4 + ak) * x 因为 ...

  3. source 命令与“ . ”点命令

    http://wenku.baidu.com/link?url=r3_WjJwQziv5wooIiatYbIMotPHcop56ZyakNGFor5DgJLQD-orAwVmOwp80RAnJ3tRD ...

  4. [转] lib和dll 区别,生成及使用方法

    lib 和 dll 的区别.生成以及使用详解 [目录] lib dll介绍 生成动态库 调用动态库 生成静态库 调用静态库 首先介绍一下静态库(静态链接库).动态库(动态链接库)的概念,首先两者都是代 ...

  5. 【python练习】截取网页里最新的新闻

    需求: 在下面这个网页,抓取最新的新闻,按天划分. http://blog.eastmoney.com/13102551638/bloglist_0_1.html 实现方法1:使用递归 import ...

  6. 针对初学者的A*算法入门详解(附带Java源码)

    英文题目,汉语内容,有点挂羊头卖狗肉的嫌疑,不过请不要打击我这颗想学好英语的心.当了班主任我才发现大一18本书,11本是英语的,能多用两句英语就多用,个人认为这样也是积累的一种方法. Thanks o ...

  7. ZOJ 3329 - One Person Game

    题意:每次筛三个骰子面分别为k1,k2,k3,如果三个骰子的值分别为a,b,c则得分置0,否则得到分数加上三个骰子的值的和,如果得分大于等于n则结束游戏. 设E[i]表示当前得到i分时结束游戏的期望. ...

  8. (转)js的左右滑动触屏事件

    原文:http://blog.sina.com.cn/s/blog_6a0a183f0100zsfk.html (2012-01-20 08:55:53) 转载▼ 标签: 移动设备 触屏事件 杂谈 分 ...

  9. SwipeRefreshLayout

    也许之前下拉刷新你可能会用到一些第三方开源库,如PullToRefresh, ActionBar-PullToRefresh.XlistView等 但现在已经有官方的组件了---SwipeRefres ...

  10. 2016-1-6第一个完整APP 私人通讯录的实现 2:增加提示用户的提示框,监听文本框

    一:在登录时弹出提示用户的提示框: 1.使用第三方框架. 2.在登陆按钮点击事件中增加如下代码: - (IBAction)loginBtnClicked { NSString *acount = se ...