P3386 【模板】二分图最大匹配

发布时间 2024-01-06 17:37:36作者: shenshen2021

include <bits/stdc++.h>

using namespace std;
struct node {
int next,to;
}e[100010];
int top,ans,visit[100010],match[100010],head[100010];
void merge(int x,int y){
e[++top].next=head[x];
e[top].to=y;
head[x]=top;
}

bool dfs(int x){
for(int i=head[x],y;i;i=e[i].next){
if(!visit[y=e[i].to]){
visit[y]=1;
if(!match[y]|| dfs(match[y])){
match[y]=x;return 1;
}
}
}
return 0;
}
int main(){
int n,m,e;
cin>>n>>m>>e;
for(int a,b,i=1;i<=e;i++){
cin>>a>>b;
merge(a,n+b),merge(n+b,a);
}
for(int i=1;i<=n;i++){
memset(visit,0,sizeof visit);
if(dfs(i)) ans++;
}
cout<<ans;
}