Maximum matching of binary graphs--Hungarian algorithm

Source: Internet
Author: User

On the interpretation of the algorithm, I think this blog post is very good: http://blog.csdn.net/dark_scope/article/details/8880547

My Code:

1#include <cstdio>2#include <vector>3#include <cstring>4 5 using namespacestd;6 7 #defineMAXN 10058 9vector<int>G[MAXN];Ten intN, M, COLOR[MAXN], MM[MAXN]; One BOOLUSED[MAXN]; A  - BOOLIsbigraph (intXintCO) - { the     BOOLRET =true; -COLOR[X] =Co; -     intSZ =g[x].size (); -      for(intI=0; i<sz; ++i) +     { -         inty =G[x][i]; +         if(color[y]<0) A         { atret = Ret&&isbigraph (y, co^1); -             if(!ret)returnret; -         } -         Else if(Color[y]==co)return false; -     } -     returnret; in } -  to BOOLFINDM (intx) + { -     intSZ =g[x].size (); the      for(intI=0; i<sz; ++i) *     { $         inty =G[x][i];Panax Notoginseng         if(!Used[y]) -         { theUsed[y] =true; +             if(mm[y]==0||Findm (Mm[y])) A             { theMM[X] =y; +Mm[y] =x; -                 return true; $             } $         } -     } -     return false; the } - Wuyi voidInit () the { -      for(intI=1; i<=n; ++i) g[i].clear (); Wumemset (color+1, -1Nsizeof(int)); -memset (mm+1,0Nsizeof(int)); About } $  - intMain () - { -      while(SCANF ("%d%d", &n, &m)! =EOF) A     { + init (); the          while(m--) -         { $             intu, v; thescanf"%d%d", &u, &v); the G[u].push_back (v); the g[v].push_back (u); the         } -         if(Isbigraph (1,0)) in         { the             intAns =0; the              for(intI=1; i<=n; ++i) About             { the                 if(Color[i]) the                 { thememset (used+1,0, n); +                     if(Findm (i)) + +ans; -                 } the             }Bayiprintf"%d\n", ans); the         } the         ElsePuts"0"); -     } -     return 0; the}

Maximum matching of binary graphs--Hungarian algorithm

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.