记录每个节点的出度,叶子节点出度为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. Java 字符编码归纳总结

    String newStr = new String(oldStr.getBytes(), "UTF-8");       java中的String类是按照unicode进行编码的 ...

  2. 【Binary Tree Zigzag Level Order Traversal】cpp

    题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from lef ...

  3. 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

    // test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  4. LAMP环境的搭建

    [一些前言废话]一名web开发尤其是后端不懂LAMP环境的搭建,那就摊上事了,有些人说他一直用win下的wampServer这种傻瓜式环境搭建,用的挺好的,也有人说他用云服务器,搭配“一键搭建LAMP ...

  5. SPOJ-SQRBR Square Brackets

    原题传送:http://www.spoj.pl/problems/SQRBR 动态规划. 设f[i][j]表示前i个位置在合法情况下缺少j个右括号的方案数. 转移方程为: f[i][j] = f[i- ...

  6. JAVA 显示图片的简单源码 分类: Java Game 2014-08-14 10:10 77人阅读 评论(0) 收藏

    此代码的原理就是用JLabel来加载图片,再将JLabel放入JFrame中, package com.mywork; import javax.swing.ImageIcon; import jav ...

  7. POJ 2004 Mix and Build (预处理+dfs)

    题意: 给N个字符串,要求出一个序列,在该序列中,后一个串,是由前一个串加一个字母后得来的(顺序可以改动). 问最多能组成多长的序列.思路:将给的字符串排序,再对所有的字符串按长度从小到大排序,若长度 ...

  8. iOS-JS交互 (WebViewJavascriptBridge)

    , , , );     messageButton.titleLabel.font = font;     messageButton.backgroundColor = [UIColor colo ...

  9. 几点基于Web日志的Webshell检测思路

    http://www.open-open.com/lib/view/open1456751673359.html

  10. UVA 10341 二分搜索

    Solve the equation:p ∗ e−x + q ∗ sin(x) + r ∗ cos(x) + s ∗ tan(x) + t ∗ x2 + u = 0where 0 ≤ x ≤ 1.In ...