Bipartite Graph Judgment + Maximum Matching + Example HDU2444 The Accomodation of Students

Note: This article refers to the blog link : Bipartite Graph Collection

Definition of bipartite graph:
bipartite graph is also called bipartite graph, which is a special model in graph theory and a special network flow. Its biggest feature is that the vertices in the graph can be divided into two sets, and the points in the set are not directly related.
Judgment of bipartite graph:
If a graph is bipartite, then it has at least two vertices, and the length of all loops is even (or the number of nodes in the loop is even), any graph without loops is bipartite Figure.
The method of
judging the bipartite graph: the method of judging the bipartite graph: use the dyeing method to dye the points in the graph into black and white.
First take a point and dye it white, and then dye its adjacent point black. If there is an adjacent point with the same color, then exit. It can be seen that this graph is not a bipartite graph.
Matching:
1. Definition: Given a bipartite graph G, in a subgraph M of G, any two edges in the edge set {E} of M are not attached to the same vertex, then M is called a match.
Matching point: two points on the matching edge
2. Maximal Matching : It means that under the currently completed matching, it is impossible to increase the number of matching edges by adding unfinished matching edges.
3. Maximum matching (maximum matching) : It is the one with the largest number of edges among all the maximum matching, set to M. Choosing such a subset with the largest number of edges is called the maximum matching problem of the graph.
4. Perfect matching (complete matching) : All vertices in a graph are matching points, that is, 2|M| = |V|. A perfect match must be the maximum match, but not every graph has a perfect match.
5. The best match:Optimal matching is also called weighted maximum matching, which refers to finding a match in a bipartite graph with weighted edges to maximize the sum of weights on the matching edges. Generally, the number of vertices in the X and Y sets is the same, and the optimal match is also a complete match, that is, every vertex is matched. If the numbers are not equal, the conversion can be achieved by adding points and 0 edges. The KM algorithm is generally used to solve this problem. (KM (Kuhn and Munkres) algorithm is a greedy extension of the Hungarian algorithm.)
6. Minimum cover The minimum cover of
a bipartite graph is divided into minimum vertex cover and minimum path cover:
①Minimum vertex cover refers to the minimum number of vertices such that Each edge in the bipartite graph G is associated with at least one of the points.
Note: The minimum number of vertex covers of the bipartite graph = the maximum number of matches of the bipartite graph
②Minimum path coverage is also called minimum edge coverage, which means using as few as possible The intersecting simple path covers all vertices in the bipartite graph.
Note: The minimum path coverage of the bipartite graph=|V|-the maximum matching number of the bipartite graph
7. The maximum independent set The
maximum independent set refers to finding a set of points such that any two points have no corresponding edges in the graph. For general graphs, the maximum independent set is an NP-complete problem. For bipartite graphs, the maximum independent set=|V|-the maximum matching number of the bipartite graph. The maximum independent set S is complementary to the minimum cover set T.

Example question: HDU2444 The Accomodation of Students
Question meaning: Some students have a relationship between cognition and ignorance , but this relationship is not transmitted. Ask whether the students can be divided into two groups, the students in each group do not know each other (that is, bipartite graph judgment). If possible, put students who know each other in a double room and ask how many double rooms are needed at most (ie the maximum matching of the bipartite graph).
Solution: Use the coloring method to determine the bipartite graph. You can use the Hungarian algorithm to find the maximum matching graph. If you don't, you can learn it.
Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
using namespace std;
typedef long long  ll;
const int MAXN=1e4+5;
const int mod=1e9+7;
const int INF =0x3f3f3f;
int n,m;
int e[205][205];
int vis[205];//标记为染黑色或者白色
int match[205];
bool istow()//判断是否为二分图
{
    
    
    memset(vis,0,sizeof vis);
    queue<int>q;
    q.push(1);
    vis[1]=1;
    while(!q.empty())
    {
    
    
        int now=q.front();
        q.pop();
        for(int i=1;i<=n;i++)
        {
    
    
            if(e[now][i]==1)
            {
    
    
                if(vis[i]==0)//未染色,则进行染色
                {
    
    
                    if(vis[now]==1) vis[i]=2;//与now点的颜色相反
                    else vis[i]=1;
                    q.push(i);
                }
                else if(vis[i]==vis[now])
                {
    
    
                    return false;//若两个点颜色相等,则不是二分图。
                }
            }
        }
    }
    return true;
}
int finder(int i)//寻找最大二分匹配图(匈牙利算法)
{
    
    
    for(int j=1;j<=n;j++)
    {
    
    
        if(!vis[j]&&e[i][j]==1)
        {
    
    
            vis[j]=1;
            if(match[j]==0||finder(match[j]))
            {
    
    
                match[j]=i;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    
    
    while(scanf("%d%d",&n,&m)!=EOF)
    {
    
    
        memset(e,0,sizeof e);
        int a,b;
        for(int i=1;i<=m;i++)
        {
    
    
            scanf("%d%d",&a,&b);
            e[a][b]=1;
            e[b][a]=1;
        }
        if(!istow()||n==1)
        {
    
    
            printf("No\n");
            continue;
        }
        memset(match,0,sizeof match);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
    
    
            memset(vis,0,sizeof vis);
            ans+=finder(i);
        }
        printf("%d\n",ans/2);
    }
}

Guess you like

Origin blog.csdn.net/weixin_45755679/article/details/107403097