ACM PKU 1659 Frogs' Neighborhood
题目链接:http://poj.org/problem?id=1659
这里介绍一个定理:Havel-Hakimi定理;
由非负整数组成的非增序列s:d1,d2,d3... ...,dn(n>=2,d1>=1)是可图的,当且仅当序列
s1:d2-1,d3-1... ... d(d1+1)-1,d(d1+2),...,dn是可图的。序列s1中有n-1个非负整数,s序列中d1后的前d1个度数(即d2~d(d1+1))减1后构成s1中的前d1个数。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
struct node
{
int id,w;
} A[100];
int T,N;
int C[100][100];
bool operator < (const node &a,const node &b)
{
return a.w>b.w;
}
int main()
{
//freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
memset(C,0,sizeof(C));
bool flag = true;
scanf("%d",&N);
for(int i=0; i<N; i++)
{
A[i].id=i;
scanf("%d",&A[i].w);
}
for(int i=0; i<N; i++)
{
sort(A+i,A+N);
if(A[i].w==0) break;
if(A[i].w>N)
{
flag=false;
break;
}
for(int j=1; j<=A[i].w; j++)
{
A[i+j].w--;
C[A[i].id][A[i+j].id]=1;
C[A[i+j].id][A[i].id]=1;
if(A[i+j].w<0)
{
flag=false;
break;
}
}
if(!flag) break;
}
if(!flag) printf("NO\n");
else
{
printf("YES\n");
for(int i=0; i<N; i++)
{
for(int j=0; j<N; j++)
{
printf("%d ",C[i][j]);
}
putchar('\n');
}
}
putchar('\n');
}
return 0;
}
浙公网安备 33010602011771号