Template://hdu 2063
Hopcroft-carp time complexity of O (sqrt (V) *e);
And the Hungarian algorithm is O (v*e);
#include <stdio.h>#include <ctime>#include <math.h>#include <limits.h>#include <complex>#include <string>#include <functional>#include <iterator>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <set>#include <map>#include <list>#include <bitset>#include <sstream>#include <iomanip>#include <fstream>#include <iostream>#include <ctime>#include <cmath>#include <cstring>#include <cstdio>#include <time.h>#include <ctype.h>#include <string.h>#include <assert.h>using namespace STD;Const intN =1005;Const intINF =1<< -;intG[n][n];intMx[n];intMy[n];intDx[n];intDy[n];BOOLUsed[n];intNx, Ny, dis;BOOLSEARCHP () {dis = INF;intI, V, u;STD:: Queue<int>Q;memset(DX,-1,sizeof(DX));memset(Dy,-1,sizeof(DY)); for(i =0; i<nx; i++) {if(Mx[i] = =-1) {Q.push (i); Dx[i] =0; } } while(! Q.empty ()) {u = Q.front (); Q.pop ();if(Dx[u]>dis) Break; for(v =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) {intV for(v =0; v<ny; v++) {if(G[u][v] &&!used[v] && dy[v] = Dx[u] +1) {Used[v] =true;if(My[v]! =-1&& dy[v] = = dis)Continue;if(My[v] = =-1|| DFS (My[v])) {My[v] = u; Mx[u] = v;return true; } } }return false;}intHungary () {intUintRET =0;memset(Mx,-1,sizeof(Mx));memset(My,-1,sizeof(My)); while(SEARCHP ()) {memset(Used,false,sizeof(used)); for(U =0; u<nx; u++)if(Mx[u] = =-1&& DFS (U)) ret++; }returnRET;}intMain () {intK, u, v; while(~scanf("%d", &k), k) {scanf("%d%d", &nx, &ny);memset(g,0,sizeof(g)); Ny = Nx>ny? Nx:ny; while(k--) {scanf("%d%d", &u, &v); u--; v--; G[U][V] =1; }intans = Hungary ();printf("%d\n", ans); }return 0;}
Hopcroft-carp algorithm Template "dichotomy graph matching"