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]放入优先队列。
先把所有叶子放到优先队列中。
从左往右遍历给定序列,对于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; }
相关文章推荐
- URAL1069 Prufer Code
- ural 1069. Prufer Code
- URAL 1069 Prufer Code(模拟)
- URAL 1069 Prufer Code 树结构脑洞题
- ural 1069. Prufer Code
- URAL 1069 Prufer Code
- ural 1306. Sequence Median(优先队列)
- ural 1069
- 1069. Prufer Code
- ural 1069
- URAL 1306. Sequence Median(优先队列)
- URAL1069
- ural 1069. Prufer Code
- URAL 1069. Prufer Code 读懂题意逆推模拟
- URAL - 1069 Prufer Code (图的重建)
- hdu1062Ignatius and the Princess I(优先队列+bfs)
- URAL 2052 . Physical Education (数位DP + 二分)
- ural 1002 Phone Numbers 字典树+dp
- ural 1008. Image Encoding bfs
- Ural 1109 Conference(最小路径覆盖数)