HDU - 1083 Courses (匈牙利算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1083

匈牙利算法看起来比较简单,就是若找到后来者匹配点未配对直接返回配对成功,若找到的后来者匹配点已经被匹配,则取找该点的配对者是否能换点配对,如果能则后来者成功配对,前者则更换了配对点,否则后来者配对失败。因此可以在确保已经配对成功的保持配对成功的状态下,来判断后来者能否成功配对,据此递归。

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 310;
int T, P, N, count, st;
int G[maxn][maxn], vis[maxn], match[maxn];
bool dfs(int x) {
	for (int i = 1; i <= N; i++) {
		if (!vis[i] && G[x][i]) {
			vis[i] = 1;
			if (!match[i] || dfs(match[i])) {
				match[i] = x;
				return true;
			}
		}
	}
	return false;
}
int main(void) {
	scanf("%d", &T);
	while (T--){
		scanf("%d%d", &P, &N);
		memset(G, 0, sizeof(G));
		memset(match, 0, sizeof(match));
		for (int i = 1; i <= P; i++) {
			scanf("%d", &count);
			while (count--){
				scanf("%d", &st);
				G[i][st] = 1;
			}
		}
		int flag = 0;
		for (int i = 1; i <= P; i++) {
			memset(vis, 0, sizeof(vis));
			if (!dfs(i)) { flag = 1; break; }
		}
		printf("%s\n", flag ? "NO" : "YES");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JILIN.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值