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. equals()和hashcode()详解

    转载自http://www.cnblogs.com/Qian123/p/5703507.html java.lang.Object类中有两个非常重要的方法:   public boolean equa ...

  2. Mac10.11.2 Apache 服务配置

    系统默认是隐藏apache安装目录的,但我们可以通过“命令行”或者“文件夹前往”的方式找到它.它是安装在系统的私有目录下,也就是/private/etc下面,因为它是隐藏的,所以我们无法通过界面找到它 ...

  3. 对小组项目alpha发布的评价

    第一组:新蜂小组 项目:俄罗斯方块 评论:看见同学玩的时候,感到加速下落时不是很灵敏,没有及成绩的功能,用户的界面仍在修正. 第二组:天天向上 项目:连连看 评论:这个游戏增加了很多好玩的功能,比如更 ...

  4. mybatis update数据时无异常但没更新成功;update异常时如数据超出大小限制,造成死锁

    没更新的问题原因: sqlSession.commit(); 没执行commit,但官方文档里有这样的描述:“默认情况下 MyBatis 不会自动提交事务,除非它侦测到有插入.更新或删除操作改变了数据 ...

  5. Geek荣耀大会总结

    0.0 首先没有被抽中, 其次可乐真难喝,再次我没有去拍无人机合影,再再次还是很受打击的. 1.0 其实 对geek 和1024大会无感,主要原因 没有三倍加班费的节日在我眼里都不是节日. 上面只是简 ...

  6. linux的一些机制Signal, Fork,

    signal(SIGCHLD, SignalHandler); 注册软中断,对应的api close(socket); ret=fork(): 父进程,返回子进程的pid. 子进程返回0, 出错返回& ...

  7. PHP《将画布(canvas)图像保存成本地图片的方法》

    用PHP将网页上的Canvas图像保存到服务器上的方法 2014年6月27日 歪脖骇客 发表回复 8 在几年前HTML5还没有流行的时候,我们的项目经理曾经向我提出这样一个需求:让项目评审专家们在评审 ...

  8. Ubuntu的IP地址配置

    概况和需求: 我的主机上有两块网卡,识别后分别是eth0和eth1.eth0配置需要为静态ip,eth1配置为使用动态主机协议获取ip地址. 步骤: 首先碰到的一个问题就是不知道eth0和eth1对应 ...

  9. Android 混淆签名打包

    1.混淆文件 proguard-rules.pro # Add project specific ProGuard rules here. # By default, the flags in thi ...

  10. QoS专题-第5期-QoS实现之队列调度与报文丢弃

    QoS实现之队列调度与报文丢弃 前面的专题里面介绍了通过优先级映射或者MQC对报文进行分类和标记,那么分类之后要做什么呢?那就是根据分类结果对报文进行流量控制处理.拥塞管理和拥塞避免就是两种流量控制手 ...