题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1191
题解:
裸匈牙利,注意如果出现找不到增广路的情况就直接break
1 #include<cstdio> 2 #include<cstring> 3 #define MAXN 1010 4 int n,m,cnt,match[MAXN*2],head[MAXN*2],ans; 5 bool check[MAXN*2]; 6 struct node 7 { 8 int v,next; 9 }edge[MAXN*4]; 10 void add(int x,int y) 11 { 12 edge[++cnt].next=head[x]; 13 head[x]=cnt; 14 edge[cnt].v=y; 15 } 16 bool Hungary(int u) 17 { 18 for(int i=head[u];i!=0;i=edge[i].next) 19 { 20 int v=edge[i].v; 21 if(!check[v]) 22 { 23 check[v]=true; 24 if(!match[v]||Hungary(match[v])) 25 { 26 match[v]=u; 27 return true; 28 } 29 } 30 } 31 return false; 32 } 33 int main() 34 { 35 scanf("%d%d",&n,&m); 36 for(int i=1;i<=m;i++) 37 { 38 int x,y; 39 scanf("%d%d",&x,&y); 40 add(i,x+m+1); 41 add(i,y+m+1); 42 } 43 for(int i=1;i<=m;i++) 44 { 45 memset(check,false,sizeof(check)); 46 if(Hungary(i))ans++; 47 else break; 48 } 49 printf("%d ",ans); 50 return 0; 51 }