URAL 1069 Prufer Code 优先队列

记录每个节点的出度,叶子节点出度为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 ;
}
上一篇:centos 7.0 phpize 扩展php


下一篇:Python之函数篇