http://acm.hdu.edu.cn/showproblem.php?pid=2063
二分图最大匹配,参照白书上邻接表建的图
View Code
#include <stdio.h> #include <string.h> const int MAX1=501; const int MAX2=1001; int k,m,n; int match[MAX1]; int u[MAX2],v[MAX2]; int first[MAX1],next[MAX2]; int vis[MAX1]; int find(int ue) { for(int e=first[ue];e!=-1;e=next[e]) if(!vis[v[e]]) { vis[v[e]]=1; if(match[v[e]]==-1||find(match[v[e]])) { match[v[e]]=ue; return 1; } } return 0; } int max_match() { int ans=0; memset(match,-1,sizeof(match)); for(int e=1;e<=m;e++) { memset(vis,0,sizeof(vis)); ans+=find(e); } return ans; } void read_graph() { memset(first,-1,sizeof(first)); for(int e=0;e<k;e++) { scanf("%d%d",&u[e],&v[e]); next[e]=first[u[e]]; first[u[e]]=e; } } int main() { while(scanf("%d%d%d",&k,&m,&n),k) { read_graph(); int ans=max_match(); printf("%d\n",ans); } return 0; }