Binary graph maximum matching (HK)

Source: Internet
Author: User

Binary graph maximum Matching classic algorithm is the Hungarian algorithm, but this article is not about Hungarian algorithm, but a more sophisticated time-complexity of the HK algorithm.
The X-square point is defined first, and the Y-square point is a different two-party point in the two-minute chart.
Implementation process:
1. Add all the non-covered points in the X-square point to the queue.
2. Carry out a wide search to find a short way to widen.
The process is as follows:

    1> each visit, find the point in the Y-square point that is not labeled, and set its label to the X-square-point designator +1.
    2> If the selected Y-square point is an open point, you find the "augmented path" and do not continue searching for this line.
    3> If the selected Y-square point matches the point, continue searching along that path. At the same time, the label of its matching x-square point is set to the Y-square-point label +1.

3, find the non-covered point, the Hungarian algorithm searches for the augmented path, but the point of access must be the label of the previous point +1.

Code:
Where DX is the mark of X Square Point, DY is the marking of y Square Point, Linkx is the match point of X Square Point, and Linky is the matching point of Y Square point.

/* Written by Tyx_yali 2017.02.09 * * #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #define for (AA,BB,CC) for (int aa=bb;aa<=cc;++aa) #define SET (AA,BB) memset (AA
, bb,sizeof (AA)) using namespace Std;
const int maxn=250010;
int N,m,s,ans;
int be[maxn],ne[maxn],to[maxn],e;
int LINKX[MAXN],LINKY[MAXN];

int DX[MAXN],DY[MAXN];
    void Add (int x,int y) {to[++e]=y,ne[e]=be[x],be[x]=e;
if (!linkx[x] &&!linky[y]) Linkx[linky[y]=x]=y,++ans;
    } bool BFs () {bool flag=0;
    int q[maxn],f=0,l=0;
    Set (dx,0), set (dy,0);
    for (I,1,n) {if (!linkx[i]) q[++l]=i;
        } while (f<l) {int k=q[++f];
            for (int i=be[k];i;i=ne[i]) {int u=to[i];
                if (!dy[u]) {dy[u]=dx[k]+1;
                if (!linky[u]) flag=1;
            else Dx[linky[u]]=dy[u]+1,q[++l]=linky[u];
}}} return flag; } bool Dfs (int node) {for (int i=be[Node];i;i=ne[i]) {int u=to[i];
            if (dy[u]==dx[node]+1) {dy[u]=0;
                if (!linky[u] | | DFS (LINKY[U)) {linkx[linky[u]=node]=u;
            return 1;
}}} return 0;
    } void Work () {scanf ("%d%d%d", &n,&m,&s);
        for (i,1,s) {int x, y;
        scanf ("%d%d", &x,&y);
    Add (x, y);
        } while (BFS ()) {for (i,1,n) {if (!linkx[i] && dfs (i)) ++ans;
    }} printf ("%d\n", ans);
    for (i,1,n) {printf ("%d", linkx[i]);

} puts ("");}
    int main () {work ();
return 0;
 }

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.