【模板-图论】二分图最大匹配 洛谷 P3386
作者:
acwing_kai
,
2020-09-11 22:58:32
,
所有人可见
,
阅读 698
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10005, maxm = 200005;
int n, m, e, tot, ans;
int head[maxn], ver[maxm], Next[maxm], match[maxn];
bool vis[maxn];
void add(int x, int y){
ver[++ tot] = y;
Next[tot] = head[x], head[x] = tot;
}
bool dfs(int x){
for(int i = head[x], y; i; i = Next[i]){
if(!vis[y = ver[i]]){
vis[y] = 1;
if(!match[y] || dfs(match[y])){
match[y] = x; return true;
}
}
}
return false;
}
int main(){
cin >> n >> m >> e;
for(int i = 1, x, y; i <= e; ++ i){
cin >> x >> y;
add(x, y + n);
add(y + n, x);
}
for(int i = 1; i <= n; ++ i){
memset(vis, 0, sizeof(vis));
if(dfs(i)) ans ++;
}
cout << ans << endl;
}