二分图匹配(入门篇)

二分图匹配

  • write by BigYellowDog

1. 什么是二分图?

  • 有一个无向图,如果所有的点可以被所有的边分成两个点集。则说这个图为二分图
  • 下图就是一个标准二分图:

2. 什么是二分图匹配?

  1. 现有一个二分图E,还有它的子集M。如果M中任意一条边都没有公共的端点。则M是一个“匹配”
  2. 如果一个匹配M’中边数是所有匹配中最多的,则说M’为“最大匹配”
  3. 如果匹配M中,所有点都得到了一一匹配,则说M是一个“完全匹配”

  • 如上图(不用管最下面那一行字)。如果红黑线一起看的画,这个图就是一个二分图。如果只看红线的话,那么其所构成的图就是二分图的一个匹配。同时,它也是一个最大匹配(每个点都得到了一一匹配)。

3. 二分图的实际应用?

  • 举个简单的例子。
  • 一个学校需要分配教学任务,而且要使每科都尽量有老师教,每个老师都尽量有课教。
  • 那么将老师抽象成一个点集,将课程抽象成另一个点集,进行二分图匹配算法,求出最大匹配。

4. 如何实现二分图匹配?

  1. 跑最大流dinic O( \(n\sqrt{m}\) )
  2. 匈牙利算法 O (nm)
  • 那么我这篇文章就只写匈牙利,因为好写且好短。最大流这个坑以后我会填的(逃
  • 噢还有上面那么的复杂度分析n是点数,m是边数
  • 匈牙利算法的流程图解网上大把多好文章。那么我就推荐两个我当时看的吧
  1. 洛谷模版题题解
  2. 匈牙利算法详解

5. 模版题(Luogu P3386)

  • 贴出我的代码,里面有详细注释~
//阅读程序之前,请确保你阅读了我上面推荐的两个文章。
//因为本文视角是以“男女配对”看的(逃
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 1005
using namespace std;

int n, m, q, ans;
int rel[maxn][maxn];
int mat[maxn];
bool vis[maxn];

bool dfs(int x)
{
    for(int j = 1; j <= m; j++)
        if(!vis[j] && rel[x][j])
        //如果以前j这个女生跟x这个男生没有联系过并且x这个男生喜欢j这个女生
        {
            vis[j] = 1;
            if(!mat[j] || dfs(mat[j]))
            //如果这个女生没有人跟她配对
            //或者她曾经配对成功的人可以跟另一个人私奔(那么这个女生不就没人配对了嘛)
            {
                mat[j] = x;
                return 1;
            }
        }
    return 0;
}

int main()
{
    cin >> n >> m >> q;
    for(int i = 1; i <= q; i++)
    {
        int u, v;   cin >> u >> v;
        if(v > m || u > n) continue;
        rel[u][v] = 1;
        /*
            * 这里是建单向边。想想我们的配对过程,都是只关注一个点集
            * 相对与另一个点集的情况
        */
    }

    for(int i = 1; i <= n; i++)
    {
        //vis数组是标记每个女生是否以前尝试跟i这个男生配对过。
        //如果尝试配对过了,就不用再一次找她了,避免递归进入死循环
        memset(vis, 0, sizeof(vis));
        if(dfs(i)) ans++;
    }

    cout << ans;
    return 0;
}

原文地址:https://www.cnblogs.com/BigYellowDog/p/10347294.html

时间: 2024-08-07 10:43:06

二分图匹配(入门篇)的相关文章

【二分图匹配入门专题1】D - Matrix hdu2119【最小顶点覆盖】

Give you a matrix(only contains 0 or 1),every time you can select a row or a column and delete all the '1' in this row or this column . Your task is to give out the minimum times of deleting all the '1' in the matrix. InputThere are several test case

二分图匹配入门题

板子(匈牙利算法,邻接矩阵) const int MAXN=2e3+5; int uN, vN; int g[MAXN][MAXN]; int linker[MAXN]; bool used[MAXN]; bool dfs(int u) { for(int v=0; v<vN; v++) if(g[u][v] && !used[v]) { used[v]=true; if(linker[v]==-1 || dfs(linker[v])) { linker[v]=u; return t

【二分图匹配入门专题1】E - Air Raid hdu1151【最小路径覆盖】

Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town's streets you can never reach the same intersection i.e. the town's

【二分图匹配入门专题1】K - Going Home hdu1533【km匹配】

On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, unt

【二分图匹配入门专题1】B - Girls and Boys hdu1068【最大独立集】

the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved" is defined between one girl and one boy. For the study reasons it is necessary to find out the maximu

【二分图匹配入门专题1】L - Card Game hdu 3722【km算法】

Jimmy invents an interesting card game. There are N cards, each of which contains a string Si. Jimmy wants to stick them into several circles, and each card belongs to one circle exactly. When sticking two cards, Jimmy will get a score. The score of

【二分图匹配入门专题1】I - Hiding Gold light oj 1152【二分图匹配】-------------------我是终于不那么水的水题分割线------------------------

You are given a 2D board where in some cells there are gold. You want to fill the board with 2 x 1 dominoes such that all gold are covered. You may use the dominoes vertically or horizontally and the dominoes may overlap. All you have to do is to cov

【二分图匹配入门专题1】M - Cyclic Tour hdu1853【km算法--判断自重边】

There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total len

【二分图匹配入门专题1】H - Marriage Media light oj 1184【二分图最大匹配】

You run a marriage media. You take some profiles for men and women, and your task is to arrange as much marriages as you can. But after reading their bio-data you have found the following criteria. No man will marry a woman if their height gap is gre

【二分图匹配入门专题1】F - COURSES poj1469【最大匹配--匈牙利算法模板题】

Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exactly P students that satisfies simultaneously the conditions: every stude