Luogu
Hungarian algorithm
#include <iostream>#include <cstring>#include <cstdio>using namespaceStdintN, M, UU, VV, E, lnk[1005], ans;BOOLa[1005][1005], vis[1005];BOOLDfsintu) { for(intI=1; i<=m; i++) {if(A[u][i] &&!vis[i]) {Vis[i] =true;if(!lnk[i] | | DFS (LNK[I)) {Lnk[i] = u;return true; } } }return false;}intMain () {cin>>n>>m>>e; for(intI=1; i<=e; i++) {scanf ("%d %d", &uu, &VV);if(Uu>n | | vv>m)Continue; A[UU][VV] =true; } for(intI=1; i<=n; i++) {memset (Vis,0,sizeof(VIS));if(Dfs (i)) ans++; } cout<<ans<<endl;return 0;}
Fd
Build Maximum Flow Dinic
#include <iostream>#include <cstring>#include <cstdio>#include <queue>using namespaceStdstructedge{intToo, NXT, Val;} edge[4005];intK, M, N, SS, TT, CNT, hea[1005], UU, VV, Maxflow, lev[1005];Const intoo=0x3f3f3f3f;queue<int> D;voidAdd_edge (intFrointToointval) {edge[cnt].nxt = Hea[fro]; Edge[cnt].too = too; Edge[cnt].val = val; Hea[fro] = cnt++;}BOOLBFS () {memset (Lev,0,sizeof(Lev)); D.push (ss); LEV[SS] =1; while(!d.empty ()) {intX=d.front (); D.pop (); for(intI=HEA[X]; i!=-1; I=EDGE[I].NXT) {intT=edge[i].too;if(!lev[t] && edge[i].val>0) {Lev[t] = Lev[x] +1; D.push (t); } } }returnlev[tt]!=0;}intDfsintXintLim) {if(X==TT)returnLimintaddflow=0; for(intI=HEA[X]; i!=-1&& addflow<lim; I=EDGE[I].NXT) {intT=edge[i].too;if(Lev[t]==lev[x]+1&& edge[i].val>0){intTmp=dfs (t, Min (Lim-addflow, edge[i].val)); Edge[i].val = tmp; edge[i^1].val + = tmp; Addflow + = tmp; } }returnAddflow;}voidDinic () { while(BFS ()) Maxflow + = DFS (ss, oo);}intMain () { while(SCANF ("%d", &k)!=eof) {if(!k) Break; scanf"%d %d", &m, &n); SS =0; tt = m + n +1; memset (Hea,-1,sizeof(Hea)); CNT = Maxflow =0; for(intI=1; i<=k; i++) {scanf ("%d %d", &uu, &VV); Add_edge (UU, Vv+m,1); Add_edge (Vv+m, UU,0); } for(intI=1; i<=m; i++) {Add_edge (ss, I,1); Add_edge (i, SS,0); } for(intI=m+1; i<=m+n; i++) {Add_edge (I, TT,1); Add_edge (TT, I,0); } dinic (); printf"%d\n", Maxflow); }return 0;}
luogu3386 "template" binary map matching Hungarian algorithm hdu2063 roller coaster Dinic