采用匈牙利算法,求解二分图最大匹配问题
#include <bits/stdc++.h>
#define io cin.tie(0), cout.tie(0), ios::sync_with_stdio(false)
#define LL long long
#define ULL unsigned long long
#define EPS 1e-8
#define INF 0x7fffffff
#define SUB -INF - 1
using namespace std;
int G[510][510];
int match[510], reverse_boy[510];
int n, m;
bool dfs(int x)
{
for (int i = 1; i <= m; i++)
if (!reverse_boy[i] && G[x][i])
{
reverse_boy[i] = 1;
if (!match[i] || dfs(match[i]))
{
match[i] = x;
return true;
}
}
return false;
}
int main()
{
int e;
scanf("%d%d%d", &n, &m, &e);
while (e--)
{
int a, b;
scanf("%d%d", &a, &b);
G[a][b] = 1;
}
int sum = 0;
for (int i = 1; i <= n; i++)
{
memset(reverse_boy, 0, sizeof(reverse_boy));
if (dfs(i))
sum++;
}
printf("%d\n", sum);
return 0;
}