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. c# webBrowser清除缓存问题

    1.webBrowser的浏览器为ie. 2.通过清除ie缓存即可. 3.代码调用如下: public enum ShowCommands : int { SW_HIDE = , SW_SHOWNOR ...

  2. RIGHT-BICEP测试第二次程序

    根据Right-BICEP单元测试的方法我对我写的第二次程序进行了测试: 测试一:测试能否控制使用乘除 测试二:测试是否能加括号 测试三:是否可以控制题目输出数量 测试四:能否控制输出方式,选择文件输 ...

  3. U盘安装OSX

    1.插入U盘,磁盘工具,格式化U盘为Mac OS X拓展 (日志式): 2.去网站搜索recovery disk assistant,此文件大约1.1M,直接打开使用它制作启动盘,进度条完毕就完成了. ...

  4. spring框架(3)— spring集合类的注入

    1.Car.java package com.eniac.beans; public class Car { private String type; private String factory; ...

  5. HTML&CSS实体

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  6. 2."结对项目"的心得体会

    上个星期,老师给我们布置了个课堂小作业:   某公司程序员二柱的小孩上了小学二年级,老师让家长每天出30道(100以内)四则运算题目给小学生做.二柱立马就想到写一个小程序来做这件事. 这个事情可以用很 ...

  7. Qt多线程-QThreadPool线程池与QRunnable

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt多线程-QThreadPool线程池与QRunnable     本文地址:https:/ ...

  8. Sql Server统计报表案例

    场景:查询人员指定年月工作量信息 USE [Test] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER procedure [dbo ...

  9. APIO/CTSC2017游记

    5.10开坑,别问我为啥今天才开始写,前几天玩得太开心了233 5.7 坐火车坐火车,坐地铁坐地铁.其实是第一次坐地铁233.解锁了在地铁上双手玩手机不扶东西站立的姿势? 全程烧流量上QQ,拜大佬约面 ...

  10. javascript如何封装函数

    通常写js组件开发的,都会用到匿名函数的写法去封装一个对象,与外界形成一个闭包的作用域.封装,全天下漫天遍野的封装,JQuery,EXT和Prototype.js封装的是javascript,jQue ...