M - Jamie‘s Contact Groups POJ - 2289(二分 + 多重匹配)
程序员文章站
2022-03-29 20:25:18
题目链接解题思路:最大值最小,显然二分之后裸的多重匹配即可ps:其实网络流也可AC代码:// Test1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。//#include #include #include #include #include #include #include...
解题思路:
- 最大值最小,显然二分
- 之后裸的多重匹配即可
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;
}
本文地址:https://blog.csdn.net/weixin_45691711/article/details/107284416
上一篇: 好喝更要趣味十足,来猩便利pick你的“心情咖啡”
下一篇: HTTP常用请求头与请求体实例