Binary graph (Hopcroft-carp algorithm)
Brush number of the topic can also meet the two-point chart ... And still the data volume kcal the Hungarian algorithm ....
Had to from the bin God's blog to copy the template, the first will be used, and later have time to delve into its principles.
/********************************************** binary graph matching (hopcroft-carp algorithm). Initialize: g[][] adjacency matrix call: Res=maxmatch (); Nx,ny to initialize!!! Time Complexity O (v^0.5 E) suitable for binary matching with large data ***********************************************/ Const intmaxn=3001;Const intinf=1<< -;intG[maxn][maxn],mx[maxn],my[maxn],nx,ny;intDx[maxn],dy[maxn],dis;BOOLVST[MAXN];BOOLSEARCHP () {Queue<int>Q; Dis=INF; memset (DX,-1,sizeof(DX)); memset (DY,-1,sizeof(DY)); for(intI=0; i<nx;i++) if(mx[i]==-1) {Q.push (i); Dx[i]=0; } while(!Q.empty ()) { intu=Q.front (); Q.pop (); if(Dx[u]>dis) Break; for(intv=0; v<ny;v++) if(g[u][v]&&dy[v]==-1) {Dy[v]=dx[u]+1; if(my[v]==-1) dis=Dy[v]; Else{Dx[my[v]]=dy[v]+1; Q.push (My[v]); } } } returndis!=INF; } BOOLDFS (intu) { for(intv=0; v<ny;v++) if(!vst[v]&&g[u][v]&&dy[v]==dx[u]+1) {Vst[v]=1; if(my[v]!=-1&&dy[v]==dis)Continue; if(my[v]==-1||DFS (My[v])) {My[v]=u; Mx[u]=v; return 1; } } return 0; }intMaxmatch () {intres=0; memset (Mx,-1,sizeof(Mx)); memset (My,-1,sizeof(My)); while(SEARCHP ()) {memset (VST,0,sizeof(VST)); for(intI=0; i<nx;i++) if(mx[i]==-1&&dfs (i)) res++; } returnRes; }
View Code
Binary graph (Hopcroft-carp algorithm)