概括: 对面没有匹配就直接匹配,对面匹配了就强行拆散
最坏复杂度O(nm)
#include<bits/stdc++.h>
#define N 2005
using namespace std;
int n,m,e,ans,vis[N],match[N];
int first[N],next[N*N],to[N*N],tot;
int read(){
int cnt=0,f=1;char ch=0;
while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch))cnt=cnt*10+(ch-'0'),ch=getchar();
return cnt*f;
}
void add(int x,int y){
next[++tot]=first[x],first[x]=tot,to[tot]=y;
}
bool find(int x){
for(int i=first[x];i;i=next[i]){
int t=to[i];
if(!vis[t]){ //刚刚找过了就不用再找了
vis[t]=1;
if(!match[t] || find(match[t])){//没有匹配或强行拆散成功
match[t]=x; return true;
}
}
}return false;
}
int main(){
n=read(),m=read(),e=read();
for(int i=1;i<=e;i++){
int x=read(),y=read();
add(x,y);
}
for(int i=1;i<=n;i++){
memset(vis,0,sizeof(vis));
if(find(i)) ans++;
}printf("%d",ans); return 0;
}