实质就是贪心,从度数大的开始。
struct node {
int deg;
int id;
bool operator < (const node& rhs) const {
return deg > rhs.deg;
}
}p[11];
int mp[11][11];
void solve(int n) {
bool flag = true;
memset(mp, 0, sizeof mp);
int m = n;
while(m--) {
sort(p, p + n);
int _count = p[0].deg;
if (_count <= 0) break;
if (_count >= n) {
flag = false;
break;
}
for (int i = 1;i <= _count;++i) {
p[i].deg--;
mp[p[0].id][p[i].id] = mp[p[i].id][p[0].id] = 1;
if (p[i].deg < 0) {
flag = false;
}
}
p[0].deg = 0;
}
if (!flag) puts("NO");
else {
puts("YES");
for (int i = 0;i < n;++i) {
for (int j = 0;j < n;++j)
printf("%d%c", mp[i][j], j == n - 1?'\n':' ');
}
}
}
int main(int argc, const char * argv[])
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
// clock_t _ = clock();
int t, n;
cin >> t;
while(t--) {
scanf("%d", &n);
for (int i = 0;i < n;++i)
cin >> p[i].deg, p[i].id = i;
solve(n);
if (t) puts("");
}
// printf("\nTime cost: %.2fs\n", 1.0 * (clock() - _) / CLOCKS_PER_SEC);
return 0;
}