AcWing 861. 二分图的最大匹配
原题链接
简单
作者:
DengLuo
,
2021-01-13 22:31:09
,
所有人可见
,
阅读 301
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 510, M = 100010;
int h[N], e[M], ne[M], idx = 0;
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
int match[N];
bool vis[N];
int n1, n2, m;
bool find(int x) {
for (int i = h[x]; ~i; i = ne[i]) {
int j = e[i];
if (!vis[j]) {
vis[j] = 1;
if (!match[j] || find(match[j])) {
match[j] = x;
return true;
}
}
}
return false;
}
int main()
{
memset(h, -1, sizeof h);
scanf("%d%d%d", &n1,&n2, &m);
while(m --) {
int a, b;
scanf("%d%d", &a, &b);
add(a, b);
}
int res = 0;
for (int i = 1; i <= n1; i ++) {
memset(vis, 0, sizeof vis);
if (find(i)) res ++;
}
printf("%d\n", res);
return 0;
}