题目:二分图匹配
参考:神犇的博客
代码:
#include<bits/stdc++.h>
using namespace std;
#define maxn 2000
int n,m,e;
bool a[maxn+5][maxn+5];
int match[maxn+5];
bool use[maxn+5];
void init() {
memset(a,0,sizeof(a));
memset(match,0,sizeof(match));
}
void readin() {
scanf("%d%d%d",&n,&m,&e);
for(int i=1; i<=e; i++) {
int x,y;
scanf("%d%d",&x,&y);
if(y>m) i--,e--;
else a[x][y]=true;
}
}
bool dfs(int x) {
for(int i=1; i<=n; i++) {
if(a[x][i]&&!use[i]) {
use[i]=true;
if(!match[i]||dfs(match[i])) {
match[i]=x;
return true;
}
}
}
return false;
}
int main() {
init();
readin();
int ans=0;
for(int i=1; i<=n; i++) {
memset(use,0,sizeof(use));
if(dfs(i)) ans++;
}
printf("%d",ans);
return 0;
}