PKU 1659 Frogs' Neighborhood

来源:互联网 发布:淘宝 装修 编辑:程序博客网 时间:2024/04/30 18:19


 转载请注明出处:http://blog.csdn.net/drecik__/article/details/7396982

/*  * 简单的Havel定理的应用, 不懂的可以百度下这个定理; * 有些人暴力也过了, 如果数据多的话, 就不一定能过了; */#include <iostream>#include <algorithm>using namespace std;#define MAXN 15int 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;}}elsecout << "NO" << endl;if ( t != 0 )cout << endl;}return 0;}