[Bipartite graph preliminary] [template] bipartite graph matching

Topic Link

DESCRIPTION Title: is a bipartite graph board.

Q: What is a bipartite graph it?

A: no bipartite graph is a directed graph can be divided into two subsets, and to meet this figure are the endpoints of each edge belong to different subsets.

What does that mean? The above might seem obscure, in fact, this may well be explained by life. In real life, each class has so few on the "relationship between abnormal" men and women. We will all throughout the year such people pull out stand together, if we find there is no one pair of the same sex (laughs), then that which is in line with a group of public morality (I no discrimination against homosexuals) students puppy love, we can say that the group is consistent with the definition of a bipartite graph. This is the equivalent of all a diagram feelings between them is connected to the edge of their different gender different subsets, two disjoint subsets (hermaphrodite grass too), and each side is connected to endpoint is not in the same subset (if in case there are gay friends).

After understanding the definition of a bipartite graph, then the bipartite graph that interesting things will happen? We still rely on the above model. First, the definition of a word: matching may also be referred engagement of an edge and its two points of said attached. In this model, the equivalent of a couple of meaning. A map where you can have a lot of matches, just like a year where there may be many couples is the same (not puppy love!), Then a figure matched to meet the nature of disjoint, the largest number of Matching called maximum matching . This interpretation is also very simple, after the couple had to pull out, we will find some slag male / female, and they fell in love may have two or more (too much grass), this time we break out of jealousy, these requirements people immediately make a decision for life choices, that is, the establishment of mutual relations, to become match. Disjoint match is decided because the life there is a triangular relationship is not allowed.

So in order for these students to be happy (puppy love is not happy!), We have to find the largest subset. then what should we do? This time we need to use an algorithm - Hungary algorithm.

We introduce a definition: augmenting paths. However, due to the very complex (interested own investigation), I'll just make emotional interpretation. First, we have some of the tags: Determine relationship with someone, and if someone is in the process of exchange. The following explanation by the boys to choose girls (and vice versa) is, first of all, for a boy, he chose to start, he searched and searched, then there are two situations encountered: 1 looking for 2. the girls have chosen not choose. 1, then we have to fight for their own happiness some opportunities, so let someone else try the following request for a boyfriend, then the time to ask fellow men, you can find a? (At this point the girls have been in the process of exchange, so fellow men can not find her), if we can change (the return value of a function), so happy to win this one (too Crosses grass), then you and I relations confirm (booklet, change the name?), and then returns 1 (because there may be others I for one, give people an answer), can not change only to find other people. 2 direct election just fine (after all, still pulls).

Then the algorithm part is over, a point to note: When this function is not being called ourselves but the main function calls (that is, not someone else let me change, but I am looking for), be sure to clear the array of tags! (I.e., whether a flag in the exchange process), and this tag is only one tag which is selected on the list. (To ask why then is the role of this array is that let the boys looking confused, to which he pointed out is untouchable, because people are waiting for change)

Code

#include<iostream>
#include<cstdio>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
int read(){
    char ch;
    int res=0,f=1;
    ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        res=res*10+(ch-'0');
        ch=getchar();
    }
    return res*f;
}
const int MAXN=20005;
int n,m,e,u,v,ans;
bool is_to[MAXN][MAXN],vis[MAXN];
int l[MAXN],r[MAXN];
bool dfs(int x){
    for(int i=1;i<=m;++i){
        if(!vis[i]&&is_to[x][i]){
            vis[i]=1;
            if(!r[i]||dfs(r[i])){
                r[i]=x;
                l[x]=i;
                return 1;
            }
        }
    }
    return 0;
}
int main(){
    n=read();m=read();e=read();
    for(int i=1;i<=e;++i){
        u=read();v=read();
        if(u<=n&&v<=m)is_to[u][v]=1;
    }
    for(int i=1;i<=n;++i)if(!l[i]){
        memset(vis,0,sizeof(vis));
        ans+=dfs(i);
    }
    cout<<ans;
    return 0;
}
View Code

Hungary DFS algorithm is part of it.

 

Guess you like

Origin www.cnblogs.com/clockwhite/p/11564465.html