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
这题看了很久,一直犹豫着不知道要怎么输入,以前做题一般都是逐个例子输入的,此题仿佛不是。。。后来发现是一次过输入所有数据,然后在处理。
搜索关键词Prufer Code,在维基能发现现成的算法,不过本人用的是笨一点的思路。
思路:对于输入的数列node,遍历之,对于node[i],在他前面未被删除而在他后面不再出现的最小序号对应的节点是node[i]的儿子。一开始用flag数组标记每一个节点的序号在数列中出现的次数,当遍历到node[i]时,若有最小的序号j对应的flag为0,j即与i相邻。由于输出儿子要有序,故而每个节点对应一个最小堆。但由于使用priority_queue是默认从大到小的,故而要自定义比较运算符。
 
AC Code:
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring> using namespace std; const int MAXN = ;
int node[MAXN];
int flag[MAXN]; //数字i在数列中出现了flag[i]次 struct Cmp //自定义运算符
{
bool operator() (const int& x, const int& y)
{
return x > y;
}
}; priority_queue<int, vector<int>, Cmp> son[MAXN]; int main()
{
memset(flag, , sizeof(flag));
int n = ; //n-顶点数
//输入
while(~scanf("%d", &node[n]))
{
flag[node[n++]]++;
}
//处理:算出每一个节点的儿子,存于堆中
for(int i = ; i < n; i++)
{
for(int j = ; j <= n; j++)
{
if(!flag[j])
{
flag[j] = -; //-1表明该节点已经删除
son[node[i]].push(j);
son[j].push(node[i]);
break;
}
}
flag[node[i]]--;
}
//输出结果
for(int i = ; i <= n; i++)
{
printf("%d:", i);
while(!son[i].empty())
{
printf(" %d", son[i].top());
son[i].pop();
}
puts("");
}
return ;
}

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(模拟)

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

  3. URAL 1069 Prufer Code 优先队列

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

  4. Code the Tree(图论,树)

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

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

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

  6. Code the Tree

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

  7. POJ Code the Tree 树的pufer编号

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

  8. 第七届河南省赛G.Code the Tree(拓扑排序+模拟)

    G.Code the Tree Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 35  Solved: 18 [Submit][Status][Web ...

  9. 【转】ACM 2567 -- 树的Prufer编码

    本文介绍北京大学ACM网站2567号题目的解法.介绍部分基本翻译自网站上的题目介绍. 题目介绍:    给定一棵各节点编号为整数1,2,3...n的树(例如,无环连通图),其Prufer编码(Pruf ...

随机推荐

  1. 判断字符串中是否存在的几种方案:string.indexof、string.contains、list.contains、list.any几种方式效率对比

    我们在做项目时,可能会遇到这样的需求,比如判断,1,2,3,33,22,123, 中是否存在,3,. var str=",1,2,3,33,22,123,"; 一般有几种方式: 1 ...

  2. 2nd scrum站立会议

    scrum站立会议 站立会议是让团队成员每日面对面站立互相交流他们所承担任务的进度.它的一个附带好处是让同组成员了解到工作的情况.本质上是为了团队交流,不是会议报告. 站立会议的目的: 1.让整个团队 ...

  3. (五)Jmeter中的属性和变量

    一.Jmeter中的属性: 1.JMeter属性统一定义在jmeter.properties文件中,我们可以在该文件中添加自定义的属性 2.JMeter属性在测试脚本的任何地方都是可见的(全局),通常 ...

  4. Q3 大型科技公司季报

    1. alphabet Alphabet(谷歌母公司)今天发布了截至9月30日的2018财年第三季度财报.报告显示,Alphabet第三季度总营收为337.40亿美元,比上年同期的277.72亿美元增 ...

  5. PHP上传文件限制的大小

    修改PHP上传文件大小限制的方法 1. 一般的文件上传,除非文件很小.就像一个5M的文件,很可能要超过一分钟才能上传完.但在php中,默认的该页最久执行时间为 30 秒.就是说超过30秒,该脚本就停止 ...

  6. laya3d 文件格式

    先认识下laya3d的一些文件 导出文件说明:               ls        ---    场景文件: Json文件,包含场景中所有节点的数据信息,包含光照贴图信息          ...

  7. Mybatis 映射关系

    相比 Hibernate,Mybatis 的映射关系就显得简单了很多. 未完待续....

  8. Vue2.0 - 全局操作 Vue.set

    引:http://www.cnblogs.com/zccblog/p/7192420.html Vue.set 的作用就是在构造器外部操作构造器内部的数据.属性或者方法.比如在vue构造器内部定义了一 ...

  9. 点击除指定元素以外的任意地方隐藏js

    $(document).click(function () { $(".search_box").hide(); }); $(".resultUl").on(& ...

  10. 【Asp.Net Core】在Visual Studio 2017中使用Asp.Net Core构建Angular4应用程序

    前言 Visual Studio 2017已经发布了很久了.做为集成了Asp.Net Core 1.1的地表最强IDE工具,越来越受.NET系的开发人员追捧. 随着Google Angular4的发布 ...