6 3 3 1 1 1 2 1 3 2 1 2 3 3 1 0
3
赤裸裸的二分匹配。直接上代码。
#include<stdio.h> #include<string.h> int partner[505][505]; int visited[505],link[505]; int M,N; int find(int pos){ int i; for(i=1;i<=N;i++){ if(visited[i]==0 && partner[pos][i]==1){ visited[i]=1; if(link[i]==0 || find(link[i])){ link[i]=pos; return 1; } } } return 0; } int main(){ int K,i,a,b,ans; while(scanf("%d",&K)&&K){ memset(partner,0,sizeof(partner)); memset(link,0,sizeof(link)); scanf("%d%d",&M,&N); while(K--){ scanf("%d%d",&a,&b); partner[a][b]=1; } ans=0; for(i=1;i<=M;i++){ memset(visited,0,sizeof(visited)); if(find(i))ans++; } printf("%d\n",ans); } return 0; }