1. 程式人生 > >二分圖匹配筆記

二分圖匹配筆記

using class 算法 print mes ++ tdi cst string

匈牙利算法

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 int match[501],n,m,a,b,ans=0;
 6 bool used[501],map[501][501];
 7 bool check(int u){
 8     for(int i=1;i<=m;i++){
 9         if(map[u][i]&&!used[i]){
10             used[i]=true
; 11 if(!match[i]||check(match[i])){ 12 match[i]=u; 13 return true; 14 } 15 } 16 } 17 return false; 18 } 19 int main(){ 20 memset(map,0,sizeof(map)); 21 memset(match,0,sizeof(match)); 22 scanf("%d%d",&n,&m);
23 for(int i=1;i<=m;i++){ 24 scanf("%d%d",&a,&b); 25 map[a][b]=true; 26 } 27 for(int i=1;i<=n;i++){ 28 memset(used,0,sizeof(used)); 29 if(check(i))ans++; 30 } 31 printf("%d",ans); 32 }

二分圖匹配筆記