您的位置:首页 > Web前端

URAL 1069 Prufer Code 优先队列

2013-08-02 21:42 441 查看
记录每个节点的出度,叶子节点出度为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 = 7510;

struct LLL
{
int lf;
LLL( int nn = 0 ): 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 = 0;
int N = 0;
while ( scanf( "%d", &num[cnt] ) != EOF )
{
N = max( N, num[cnt] );
++cnt;
}

memset( vis, 0, sizeof(vis) );
for ( int i = 0; i < cnt; ++i )
++vis[ num[i] ];

for ( int i = 1; i <= N; ++i )
if ( vis[i] == 0 ) leaf.push( LLL(i) );

for ( int i = 1; i <= cnt; ++i ) Tr[i].clear();

int i, j;
for ( i = 0; 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] ] == 0 )
leaf.push( LLL( num[i] ) );
}

for ( i = 1; i <= N; ++i )
{
printf( "%d:", i );
sort( Tr[i].begin(), Tr[i].end() );
int sz = Tr[i].size();
for ( j = 0; j < sz; ++j )
printf(" %d", Tr[i][j] );
puts("");

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: