题意:给定一个图n(2 < n < 10)个结点相邻的结点个数,判断是否可图。
分析:
看到这数据范围,满满的暴搜感,那既然zrt把它放到了图论题,我们寻求一些其他的算法。
yzy神犇自己想出来了一半的正解,膜拜膜拜...
据说有个定理叫havel定理,判定过程如下:
1.对当前数列排序,使其成递减。
2.把从x(2)开始的x(1)个数字-1.
3.重复1, 2步,直到序列出现负数(不可图),或全为0(可图)。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int T, n, ok, ans[15][15];
struct X {
int to, d;
bool operator < (const X &rhs) const {
return d > rhs.d;
}
}x[15];
int main() {
scanf("%d", &T);
while(T--) {
memset(ans, 0, sizeof ans);
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", &x[i].d), x[i].to = i;
while(1) {
sort(x+1, x+1+n);
if(x[1].d <= 0) {
ok = x[n].d == 0;
break;
}
for(int i = 2; i <= x[1].d + 1; i++) x[i].d--, ans[x[1].to][x[i].to] = 1, ans[x[i].to][x[1].to] = 1;
x[1].d = 0;
}
if(ok) {
puts("YES");
for(int i = 1; i <= n; i++, printf("\n"))
for(int j = 1; j <= n; j++)
printf("%d ", ans[i][j]);
} else puts("NO");
printf("\n");
}
}