引出匈牙利算法:
int find(int x, int n){
int i;
for(i = 1; i <= n; i++){
if(!vis[i] && map[x][i]){ //依次判断女生是否对男生有好感
vis[i] = 1;
if(pri[i] == -1 || find(pri[i], n)){
pri[i] = x;
return 1;
}
}
}
return 0;
}
具体见:https://blog.csdn.net/dark_scope/article/details/8880547
#include<stdio.h>
#include<string.h>
#define maxn 505
int map[maxn][maxn];
int pri[maxn];
int vis[maxn];
int find(int x, int n);
int main()
{
int k, m, n;
while(~scanf("%d", &k) && k){
scanf("%d %d", &m, &n);
int i, a, b, ans = 0;
memset(map, 0, sizeof(map));
memset(pri, -1, sizeof(pri));
for(i = 1; i <= k; i++){
scanf("%d %d", &a, &b);
map[a][b] = 1;
}
for(i = 1; i <= m; i++){
memset(vis, 0, sizeof(vis));
if(find(i, n))
ans++;
}
printf("%d\n", ans);
}
return 0;
}
int find(int x, int n){
int i;
for(i = 1; i <= n; i++){
if(!vis[i] && map[x][i]){ //依次判断女生是否对男生有好感
vis[i] = 1;
if(pri[i] == -1 || find(pri[i], n)){
pri[i] = x;
return 1;
}
}
}
return 0;
}
直接贴个代码,等懂了再回来看
较为清晰的思路:https://blog.csdn.net/thestarfish/article/details/46928267