POJ1659 Frogs' Neighborhood

本文介绍了一个利用Havel定理来解决特定图论问题的方法,该方法通过不断调整节点的度数来判断一个给定的度序列是否能够构成一个合法的无向图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:给定一个图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");
	}
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值