Frogs' Neighborhood
Time Limit: 5000MS | Memory Limit: 10000K | |||
Total Submissions: 7933 | Accepted: 3400 | Special Judge |
Description
未名湖附近共有N个大小湖泊L1, L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N)。如果湖泊Li和Lj之间有水路相连,则青蛙Fi和Fj互称为邻居。现在已知每只青蛙的邻居数目x1, x2, ..., xn,请你给出每两个湖泊之间的相连关系。
Input
第一行是测试数据的组数T(0 ≤ T ≤ 20)。每组数据包括两行,第一行是整数N(2 < N < 10),第二行是N个整数,x1, x2,..., xn(0 ≤ xi ≤ N)。
Output
对输入的每组测试数据,如果不存在可能的相连关系,输出"NO"。否则输出"YES",并用N×N的矩阵表示湖泊间的相邻关系,即如果湖泊i与湖泊j之间有水路相连,则第i行的第j个数字为1,否则为0。每两个数字之间输出一个空格。如果存在多种可能,只需给出一种符合条件的情形。相邻两组测试数据之间输出一个空行。
Sample Input
3 7 4 3 1 5 4 2 1 6 4 3 1 4 2 0 6 2 3 1 1 2 1
Sample Output
YES 0 1 0 1 1 0 1 1 0 0 1 1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 1 0 1 1 0 1 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 NO YES 0 1 0 0 1 0 1 0 0 1 1 0 0 0 0 0 0 1 0 1 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0
//Havel-Hakimi定理是根据度数来判断一组有序数据是否可以构成一个无向图。
//原理给定一个非负整数序列,每个整数代表一顶点度数,将其按非递增方式排列,然后如下:
/*
1. 去掉第一个数
2. 将后面的这个度数的个数分别减去一,然后判断是否有负数存在,如果存在便是不可构成简单无向图
3. 重新排序,回到第一步。
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct po {
int id,der;
friend bool operator<(const po &a,const po &b) {
return a.der>b.der;
}
} xr[15];
int maps[15][15];
int T,N;
bool success;
int main() {
//freopen("D://imput.txt","r",stdin);
scanf("%d",&T);
int ctmp=T-1;
while(T--) {
scanf("%d",&N);
for(int i=1; i<=N; i++) {
scanf("%d",&xr[i].der);
xr[i].id=i;
}
success=true;
memset(maps,0,sizeof(maps));
///
while(true) {
sort(xr+1,xr+N+1);
if(xr[1].der==0) {//
break;
}
int tmp=xr[1].der;
for(int i=2; i<=tmp+1; i++) {
xr[i].der--;///
if(xr[i].der<0) {
success=false;
break;
}
maps[xr[1].id][xr[i].id]=1;
maps[xr[i].id][xr[1].id]=1;
}
xr[1].der=0;
if(!success) {
break;
}
}
/
if(T!=ctmp) {
printf("\n");
}
if(success) {
printf("YES\n");
for(int i=1; i<=N; i++) {
for(int j=1; j<=N; j++) {
printf("%d",maps[i][j]);
if(j!=N) {
printf(" ");
}
}
printf("\n");
}
} else {
printf("NO\n");
}
}
return 0;
}