二分图匹配问题
匈牙利算法
贴代码:(AC,0ms)
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 1e3+5;
bool g[MAX][MAX];
int res[MAX], vis[MAX];
int k, m, n, s, t;
int find(int a){
for(int i = 1; i <= n; i++){
if(g[a][i] && !vis[i]){
vis[i] = true;
if(res[i] == 0 || find(res[i])){
res[i] = a;
return true;
}
}
}
return false;
}
int main(){
while(~scanf("%d", &k) && k){
scanf("%d%d", &m, &n);
memset(res, 0, sizeof(res));
memset(g, false, sizeof(g));
while(k--){
scanf("%d%d", &s, &t);
g[s][t] = true;
}
int ans = 0;
for(int i = 1; i <= m; i++){
memset(vis, 0, sizeof(vis));
if(find(i))
ans++;
}
printf("%d\n", ans);
}
return 0;
}