PKU 1659 Frogs' Neighborhood
2012-03-26 22:30
405 查看
转载请注明出处:/article/3712455.html
/* * 简单的Havel定理的应用, 不懂的可以百度下这个定理; * 有些人暴力也过了, 如果数据多的话, 就不一定能过了; */ #include <iostream> #include <algorithm> using namespace std; #define MAXN 15 int d[MAXN][MAXN]; struct node { int num; int d; }nodes[MAXN]; bool cmp( const node& a, const node& b ) { return a.d > b.d; } bool Havel_Hakimi( int n ) { memset( d, 0, sizeof(d)); sort( nodes, nodes+n, cmp ); while ( nodes[0].d && nodes[n-1].d >= 0 ) { int i = 1; while ( nodes[0].d-- ) { --nodes[i].d; d[nodes[0].num][nodes[i].num] = 1; d[nodes[i].num][nodes[0].num] = 1; ++i; } ++nodes[0].d; sort( nodes, nodes+n, cmp ); } if ( nodes[n-1].d < 0 ) return false; return true; } int main() { int t; cin >> t; while ( t-- ) { int n; cin >> n; for ( int i = 0; i < n; ++i ) { cin >> nodes[i].d; nodes[i].num = i; } if ( Havel_Hakimi( n ) ) { cout << "YES" << endl; for ( int i = 0; i < n; ++i ) { for ( int j = 0; j < n; ++j ) { cout << d[i][j]; if ( j < n-1 ) cout << " "; } cout << endl; } } else cout << "NO" << endl; if ( t != 0 ) cout << endl; } return 0; }
相关文章推荐
- pku1659 Frogs' Neighborhood(havel定理)
- 【Havel-Hakimi定理】PKU-1659-Frogs' Neighborhood
- PKU 1659 Frogs' Neighborhood - 贪心 Havel定理
- PKU1659(Frogs' Neighborhood)-简单图的判定+构造,Havel定理
- poj1659 Frogs' Neighborhood
- POJ-1659-Frogs' Neighborhood (Havel-Hakimi定理)
- POJ_1659 Frogs' Neighborhood
- POJ-1659-Frogs' Neighborhood
- POJ 1659 Frogs' Neighborhood 可图性判断-Havel定理
- POJ 1659 Frogs' Neighborhood (生成树问题)
- poj 1659 Frogs' Neighborhood
- poj 1659 Frogs' Neighborhood
- poj 1659 Frogs' Neighborhood(判断序列可图)
- poj Frogs' Neighborhood(1659)
- poj 1659 Frogs' Neighborhood (贪心 + 判断度数序列是否可图)
- poj 1659 Frogs' Neighborhood (判断简单图并输出)
- poj 1659 A - Frogs' Neighborhood
- poj1659 Frogs' Neighborhood 根据度数列构造图,havel_hakimi算法
- POJ 1659 Frogs' Neighborhood
- poj 1659 Frogs' Neighborhood (度序列)