Recently learned two of the maximum match, BFS template is still dead or alive to fight out? I might have learned the fake BFS.
So I used the DFS template.
The algorithm for finding the maximal matching of binary graphs is the Hungarian algorithm
The main procedure of the Hungarian algorithm is to find the augmented path
Looking for a way to shine is the process of starting from an unpaired point, after an unmatched edge, matching edge, mismatched edge, matching edge, mismatched edge 、... Finally, the process of reaching an unassigned point, as long as the path of the mismatch and matching edges of the "identity" swapped, the match is added. This is the process of looking for an augmented path that can find the maximum match by constantly looking for an augmented path.
1#include <cstdio>2#include <cstring>3#include <iostream>4 using namespacestd;5 6 structedge{7 intto,nxt;8Edge (intto=0,intnxt=0):9 to, NXT (NXT) {}Ten }; One A Const intmaxn=2005, maxm=1000005; - -Edge e[maxm<<1]; the intHEAD[MAXN],MAT[MAXN]; - BOOLCHECK[MAXN]; - intN,n_l,n_r,m,cnt=0; - + BOOLDfsintu) { - for(intE=head[u];e;e=e[e].nxt) { + intv=e[e].to; A if(!Check[v]) { atcheck[v]=1; - if(mat[v]==-1||DFS (Mat[v])) { -mat[v]=u; -mat[u]=v; - return 1; - } in } - } to return 0; + } - the voidHungarian () { * intans=0; $memset (mat,-1,sizeofmat);Panax Notoginseng for(intu=1; u<=n_l;u++) - if(mat[u]==-1){ thememset (check,0,sizeofcheck); + if(Dfs (U)) ans++; A } theprintf"%d\n", ans); + } - $InlinevoidAd_e (int from=0,intto=0){ $E[++cnt]=edge (to,head[ from]); -head[ from]=CNT; -E[++cnt]=edge ( from, Head[to]); thehead[to]=CNT; - }Wuyi the voidinit () { -scanf"%d%d%d",&n_l,&n_r,&m); Wu for(intI=0, ff,tt;i<m;i++){ -scanf"%d%d",&ff,&TT); About if(Tt>n_r)Continue; $Ad_e (ff,tt+n_l); - } - } - A intMain () { + init (); the Hungarian (); - return 0; $}
Hungarian algorithm DFS template [binary chart] [binary graph Max match]