M - Jamie‘s Contact Groups POJ - 2289(二分 + 多重匹配)

题目链接

解题思路:

  • 最大值最小,显然二分
  • 之后裸的多重匹配即可
    ps:其实网络流也可

AC代码:

// Test1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
const int maxn = 1010;
int n, m;
int graph[maxn][maxn], match[maxn][maxn], vis[maxn], cap[maxn];
void Init() {
	string s;
	char c;
	int x;
	for (int i = 1; i <= n; i++) {
		cin >> s;
		while ((c = getchar()) != '\n') {
			cin >> x;
			graph[i][x + 1] = 1;
			cap[x + 1]++;
		}
	}
}
void aftermath() {
	memset(graph, 0, sizeof(graph));
	memset(cap, 0, sizeof(cap));
}
bool dfs(int x, int mid) {
	for (int i = 1; i <= m; i++) {
		if (graph[x][i] && !vis[i]) {
			vis[i] = 1;
			if (match[i][0] < min(mid, cap[i])) {
				match[i][++match[i][0]] = x;
				return true;
			}
			for (int j = 1; j <= match[i][0]; j++)
				if (dfs(match[i][j], mid)) {
					match[i][j] = x;
					return true;
				}
		}
	}
	return false;
}
int Maxmatch(int mid) {
	int sum = 0;
	memset(match, 0, sizeof(match));
	for (int i = 1; i <= n; i++) {
		memset(vis, 0, sizeof(vis));
		if (dfs(i, mid)) sum++;
	}
	return sum;
}
int main() {
	while (cin >> n >> m && (n + m)) {
		Init();
		int lef = 1, rig = n, mid = (lef + rig) >> 1, ans;
		while (lef <= rig) {
			if (Maxmatch(mid) == n) ans = mid, rig = mid - 1;
			else lef = mid + 1;
			mid = (lef + rig) >> 1;
		}
		aftermath();
		cout << ans << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值