根据度数建立无向图(无重边)
关键词:构造法
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<queue>
#include<algorithm>
#define maxn 50
#define INF 1<<30
using namespace std;
int n,cas;
struct node{
int id,du;
}frog[maxn];
int link[maxn][maxn];
int cmp(node a,node b){
return a.du>b.du;
}
bool buildmap(){
memset(link,0,sizeof(link));
for(int i=0;i<n;i++){
sort(frog+i,frog+n,cmp);
if(frog[i].du+i>n-1) return false;//无重边
for(int j=i+1;j<=i+frog[i].du;j++){
if(!frog[j].du) return false;
frog[j].du--;
int u=frog[i].id,v=frog[j].id;
link[u][v]=1;
link[v][u]=1;
}
// frog[i].du=0;
}
return true;
}
int main(){
//freopen("a.txt","r",stdin);
scanf("%d",&cas);
while(cas--){
scanf("%d",&n);
for(int i=0;i<n;i++) frog[i].id=i;
for(int i=0;i<n;i++){
scanf("%d",&frog[i].du);
}
if(buildmap()){
printf("YES\n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
printf("%d ",link[i][j]);
}
printf("\n");
}
}
else printf("NO\n");
printf("\n");
}
}