记录每个节点的出度,叶子节点出度为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. 《JavaScript高级程序设计》第4章 变量、作用域和内存问题

    4.1 基本类型和引用类型 5种基本类型:Undefined.Null.Boolean.Number和String,这5种基本类型是按值访问的,因为可以操作保存在变量中的实际的值. 引用类型:可能由多 ...

  2. 【BZOJ】【1010】【HNOI2008】玩具装箱Toy

    DP/斜率优化 根据题目描述很容易列出动规方程:$$ f[i]=min\{ f[j]+(s[i]-s[j]+i-j-1-L)^2 \}$$ 其中 $$s[i]=\sum_{k=1}^{i} c[k] ...

  3. 【POJ】【2449】Remmarguts' Date

    K短路/A* 经(luo)典(ti) K短路题目= = K短路学习:http://www.cnblogs.com/Hilda/p/3226692.html 流程: 先把所有边逆向,做一遍dijkstr ...

  4. 使用ajax技术无刷新动态调用股票信息

    新浪的财金频道一直感觉做得很好.但由于最近网速慢的缘故,查看股票信息时网页老是打不开.这几天一直在研究ajax,于是用jquery自己做了一个自动读取新浪股票实时数据的页面. <html> ...

  5. Grid分组汇总

    Ext.onReady(function () {                Ext.define('personInfo', {                    extend: 'Ext. ...

  6. JS中 判断null

    以下是不正确的方法: var exp = null; if (exp == null) { alert("is null"); } exp 为 undefined 时,也会得到与 ...

  7. Ajax的进阶学习(一)

    在Ajax课程中,我们了解了最基本的异步处理方式.本章,我们将了解一下Ajax的一些全局请求事件.跨域处理和其他一些问题. 加载请求 在Ajax异步发送请求时,遇到网速较慢的情况,就会出现请求时间较长 ...

  8. 几个经常用到的字符串的截取(java)

    几个经常用到的字符串的截取 string str="123abc456";int i=3;1 取字符串的前i个字符   str=str.Substring(0,i); // or  ...

  9. C#加密算法汇总

    方法一: //须添加对System.Web的引用 using System.Web.Security; ... /// <summary> /// SHA1加密字符串 /// </s ...

  10. 安卓Intent.ACTION_TIME_TICK 广播

    Intent.ACTION_TIME_TICK 广播需要动态注册,不能在清单文件配置. TimeReceiver mBroadcastReceiver = new TimeReceiver(); In ...