您的位置:首页 > 其它

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