On the interpretation of the algorithm, I think this blog post is very good: http://blog.csdn.net/dark_scope/article/details/8880547
My Code:
1#include <cstdio>2#include <vector>3#include <cstring>4 5 using namespacestd;6 7 #defineMAXN 10058 9vector<int>G[MAXN];Ten intN, M, COLOR[MAXN], MM[MAXN]; One BOOLUSED[MAXN]; A - BOOLIsbigraph (intXintCO) - { the BOOLRET =true; -COLOR[X] =Co; - intSZ =g[x].size (); - for(intI=0; i<sz; ++i) + { - inty =G[x][i]; + if(color[y]<0) A { atret = Ret&&isbigraph (y, co^1); - if(!ret)returnret; - } - Else if(Color[y]==co)return false; - } - returnret; in } - to BOOLFINDM (intx) + { - intSZ =g[x].size (); the for(intI=0; i<sz; ++i) * { $ inty =G[x][i];Panax Notoginseng if(!Used[y]) - { theUsed[y] =true; + if(mm[y]==0||Findm (Mm[y])) A { theMM[X] =y; +Mm[y] =x; - return true; $ } $ } - } - return false; the } - Wuyi voidInit () the { - for(intI=1; i<=n; ++i) g[i].clear (); Wumemset (color+1, -1Nsizeof(int)); -memset (mm+1,0Nsizeof(int)); About } $ - intMain () - { - while(SCANF ("%d%d", &n, &m)! =EOF) A { + init (); the while(m--) - { $ intu, v; thescanf"%d%d", &u, &v); the G[u].push_back (v); the g[v].push_back (u); the } - if(Isbigraph (1,0)) in { the intAns =0; the for(intI=1; i<=n; ++i) About { the if(Color[i]) the { thememset (used+1,0, n); + if(Findm (i)) + +ans; - } the }Bayiprintf"%d\n", ans); the } the ElsePuts"0"); - } - return 0; the}
Maximum matching of binary graphs--Hungarian algorithm