#include <iostream>
#include <cstring>
#include <algorithm>
const int N = 510, M = 100001;//最坏 O(nm)
using namespace std;//acw 861.二分图的最大匹配
int n1, n2, m, res;
int match[N];//每个女生配对的男生
int h[N], to[M], nx[M], idx;
bool st[N];//男生是否询问过女生
void add(int a, int b)
{
to[idx] = b, nx[idx] = h[a], h[a] = idx ++;
}
bool find(int x)
{
for (int i = h[x]; i != -1; i = nx[i])
{
int j = to[i];
if (!st[j])
{
st[j] = true;
if (!match[j] || find(match[j]))
{
match[j] = x;
return true;
}
}
}
return false;
}
int main()
{
memset(h, -1, sizeof h);
cin >> n1 >> n2 >> m;
while (m -- )
{
int a, b;
cin >> a >> b;
add(a, b);
}
for (int i = 1; i <= n1; i ++ )
{
memset(st, false, sizeof st);
if (find(i)) res ++;
}
cout << res << endl;
}
[模板] 二分图最大匹配
最新推荐文章于 2024-07-23 18:35:33 发布