算法:匈牙利算法、最大流、hk算法
匈牙利算法dfs实现:
hdu2063
#include<bits/stdc++.h>
using namespace std;
int k,m,n;
int edge[505][505];
int vis[505];
int matched[505];
bool match(int x){
for(int j=1;j<=n;j++){
if(edge[x][j]&&!vis[j]){
vis[j]=1;
if(matched[j]==0||match(matched[j])){
matched[j]=x;
return true;
}
}
}
return false;
}
int main()
{
while(scanf("%d",&k)&&k!=0){
scanf("%d%d",&m,&n);
int x,y;
memset(edge, 0, sizeof(edge));
for(int i=1;i<=k;i++){
scanf("%d%d",&x,&y);
edge[x][y]=1;
}
int ans=0;
memset(matched, 0, sizeof(matched));
for(int i=1;i<=m;i++){
memset(vis, 0, sizeof(vis)); //注意每次匹配前vis数组需要清零
if(match(i)) ans++;
}
printf("%d\n",ans);
}
return 0;
}