模板题: AcWing 861. 二分图的最大匹配
输入样例:
2 2 4
1 1
1 2
2 1
2 2
输出样例:
2
AC代码:
#include <iostream>
#include <vector>
#include <memory.h>
using namespace std;
const int N = 510;
const int M = 100010;
int n1, n2, m;
int match[N];
bool st[N];
vector<int> v[2 * N];
int find(int x)
{
for (int i = 0; i < v[x].size(); i++) {
int j = v[x][i];
if (!st[j]) {
st[j] = true;
if (match[j] == 0 || find(match[j]))
{
match[j] = x;
return true;
}
}
}
return false;
}
int main()
{
cin >> n1 >> n2 >> m;
while (m--) {
int a, b;
scanf("%d%d", &a, &b);
v[a].push_back(b);
}
int res = 0;
for (int i = 1; i <= n1; i++) {
memset(st, false, sizeof(st));
if (find(i)) res++;
}
printf("%d\n", res);
return 0;
}