Hungarian algorithm DFS template [binary chart] [binary graph Max match]

Source: Internet
Author: User

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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.