匈牙利算法&模板O(mn)HDU2063
#include<cstdio> #include<cstring> #define maxn 510 using namespace std; int k,g,b,x,y,ans,girl[maxn],zou[maxn],s[maxn][maxn]; int find(int x) { for(int i=1;i<=b;i++) { if(s[x][i] && !zou[i]) { zou[i]=1; if(!girl[i] || find(girl[i])) { girl[i]=x; return 1; } } } return 0; } main(){ while(scanf("%d",&k) && k) { ans=0; memset(s,0,sizeof(s)); memset(girl,0,sizeof(girl)); scanf("%d%d",&g,&b); for(int i=1;i<=k;i++) { scanf("%d%d",&x,&y); s[x][y]=1; } for(int i=1;i<=g;i++) { memset(zou,0,sizeof(zou)); if(find(i)) ans++; } printf("%d\n",ans); } }