解题思路:
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
如对本文有疑问, 点击进行留言回复!!
灰色预测模型GM(1,1)的发展系数a=0或无限接近于0的时候预测的误差以及解决办法
网友评论