记录每个节点的出度,叶子节点出度为0,每删掉一个叶子,度数-1,如果一个节点的出度变成0,那么它变成新的叶子。

先把所有叶子放到优先队列中。

从左往右遍历给定序列,对于root[i],每次取出叶子中编号最小的那个与root[i]相连,并且--degree[ root[i] ],如果degree[ root[i] ]为0,那么把root[i]放入优先队列。

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue> using namespace std; const int MAXN = ; struct LLL
{
int lf;
LLL( int nn = ): lf(nn) { }
bool operator<( const LLL& rhs ) const
{
return lf > rhs.lf;
}
}; int vis[MAXN];
vector<int> Tr[MAXN];
int num[MAXN];
priority_queue<LLL> leaf; int main()
{
int cnt = ;
int N = ;
while ( scanf( "%d", &num[cnt] ) != EOF )
{
N = max( N, num[cnt] );
++cnt;
} memset( vis, , sizeof(vis) );
for ( int i = ; i < cnt; ++i )
++vis[ num[i] ]; for ( int i = ; i <= N; ++i )
if ( vis[i] == ) leaf.push( LLL(i) ); for ( int i = ; i <= cnt; ++i ) Tr[i].clear(); int i, j;
for ( i = ; i < cnt; ++i )
{
LLL tmp = leaf.top();
leaf.pop();
Tr[ num[i] ].push_back( tmp.lf );
Tr[ tmp.lf ].push_back( num[i] );
--vis[ num[i] ];
if ( vis[ num[i] ] == )
leaf.push( LLL( num[i] ) );
} for ( i = ; i <= N; ++i )
{
printf( "%d:", i );
sort( Tr[i].begin(), Tr[i].end() );
int sz = Tr[i].size();
for ( j = ; j < sz; ++j )
printf(" %d", Tr[i][j] );
puts(""); }
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(模拟)

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

  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. Ural 1780 Gray Code 乱搞暴力

    原题链接:http://acm.timus.ru/problem.aspx?space=1&num=1780 1780. Gray Code Time limit: 0.5 secondMem ...

  6. ural 1069

    题意:删除一棵树上的叶子 每删除一片叶子就写下连着该片叶子的节点  让你还原一棵树 记录每个节点连着的叶子数 0表示此时这个节点就是叶子  -1表示这个节点已经删除 删除的只能是0  就是说是叶子 暴 ...

  7. Code the Tree(图论,树)

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

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

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

  9. Code the Tree

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

随机推荐

  1. [转载]关于android SDK安装Failed to fetch URL http://dl-ssl.google.com/android/repository/addons_list-1.xml出错

    原文地址为:http://blog.csdn.net/springsky_/article/details/7442388 因为入行移动测试,所以很多测试环境的搭建.从中遇到了和这个GG同样的问题.怕 ...

  2. angular 嵌套实现树结构 ng-repeat ng-include

    效果图 ang.html <!doctype html><html lang="en"><head>    <meta charset=& ...

  3. 《JavaScript高级程序设计》第5章 引用类型

    5.2.2 转换方法 所有对象都有toString()和valueOf()方法调用数组的toString()方法,会返回一个字符串,由数组中的每个项通过逗号连接而成调用valueOf()还是返回数组 ...

  4. gvim编辑文件到github乱码

    with below _vimrc settings, code uploaded to GitHub will display with proper encoding set encoding=u ...

  5. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线+线段树

    题目链接: http://codeforces.com/contest/703/problem/D D. Mishka and Interesting sum time limit per test ...

  6. Ext通过后台校验字段是否重复

    话不多说,直接上代码: handlerRybh : function(textField) { Ext.Ajax.request({// ajax请求的方法 url : 'userManage/per ...

  7. ObjC的Block中使用weakSelf/strongSelf @weakify/@strongify

    首先要说说什么时候使用weakSelf和strongSelf. 下面引用一篇博客<到底什么时候才需要在ObjC的Block中使用weakSelf/strongSelf>的内容: Objec ...

  8. vs2010创建并使用DLL

    一.为什么需要dll 代码复用是提高软件开发 效率的重要途径.一般而言,只要某部分代码具有通用性,就可将它构造成相对独立的功能模块并在之后的项目中重复使用.比较常见的例子是各种应用程序框架, 如ATL ...

  9. sql server 批量删除数据表

    SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Auth ...

  10. 旨在脱离后端环境的前端开发套件 - IDT之Server篇

    IDT,一个基于Nodejs的,旨在脱离后端环境的前端开发套件,目的就是能让前端开发完全脱离后端的环境,无论后端是什么模板引擎(主流),都能应付自如. IDT主要包括两大部分:Server + Bui ...