Havel-Hakimi定理:可以由度序列判断是否能构成简单图,并输出该简单图
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxv=110;
struct Node {
int degree,v;
bool operator<(const Node& rhs)const {
return degree>rhs.degree;
}
} node[maxv];
bool mmap[maxv][maxv];
int main(void) {
#ifndef ONLINE_JUDGE
freopen("E:\\input.txt","r",stdin);
#endif // ONLINE_JUDGE
int T,n,d,x,y;
bool flag;
cin>>T;
while(T--) {
cin>>n;
memset(mmap,false,sizeof mmap);
for(int i=0; i<n; i++)
cin>>node[i].degree,node[i].v=i;
flag=1;
for(int k=0; k<n&&flag; k++) {
sort(node+k,node+n);
d=node[k].degree,x=node[k].v;
if(d>=n-k)
flag=0;
for(int j=1; j<=d&&flag; j++) {
y=node[k+j].v;
if(node[k+j].degree<=0)
flag=0;
node[k+j].degree--;
mmap[x][y]=mmap[y][x]=1;
}
}
if (flag) {
cout<<"YES"<<endl;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cout<<mmap[i][j]<<"\n "[j<n-1];
} else
cout<<"NO"<<endl;
cout<<endl;
}
return 0;
}