AcWing 861. 二分图的最大匹配(匈牙利)
原题链接
简单
作者:
Value
,
2020-05-27 09:56:26
,
所有人可见
,
阅读 550
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 510, M = 1E5 + 10;
int n1, n2, m;
int head[N], value[M], nxt[M];
int idx;
int match[N];
void add(int a, int b){
value[idx] = b;
nxt[idx] = head[a];
head[a] = idx ++ ;
}
void read(){
memset(head, -1, sizeof head);
cin >> n1 >> n2 >> m;
int a, b;
for(int i = 0; i < m; i ++ ){
scanf("%d%d", &a, &b);
add(a, b);
}
}
bool st[N];
bool find(int t){
for(int i = head[t]; i != -1; i = nxt[i]){
int p = value[i];
if(!st[p]){
st[p] = true;
if(match[p] == 0 || find(match[p])){
match[p] = t;
return true;
}
}
}
return false;
}
int hungry(){
int res = 0;
for(int i = 1; i <= n1; i ++ ){
memset(st, false, sizeof st);
if(find(i)) res ++ ;
}
return res;
}
int main(){
read();
cout << hungry() << endl;
return 0;
}