• acwing算法基础之搜索与图论--匈牙利算法求二分图的最大匹配数


    1 基础知识

    二分图中的最大匹配数:从二分图中选择一些边(这些边连接集合A和集合B,集合A中结点数目为n1,集合B中结点数目为n2),设为集合S,其中任意两条边不共用一个结点。求集合S的最大元素数目,即二分图中的最大匹配数。

    匈牙利算法的关键步骤:

    1. 初始化匹配数组match[1~n2] = 0。其中match[b] = a,表示集合B中的结点b匹配了集合A中的结点a。
    2. 遍历集合A中的每一个结点a:初始化状态数组st[1~n2] = false,其中st[b] = false表示集合B中的结点b没有被访问。然后,find(x),如果它返回true,那么答案加1。
    bool find(int a) {//a为集合A中的结点
    	for (auto b : g[x]) {
    		if (!st[b]) {//如果结点b没有被访问
    			st[b] = true;
    			if (match[b] == 0 || find(match[b])) { //如果结点b没有被匹配,或者结点b匹配了的结点可以找到新的
    				match[b] = a;
    				return true;
    			}
    		}
    	}
    	return false;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    1. 最终返回答案,即为该二分图的最大匹配数。

    2 模板

    int n1, n2;     // n1表示第一个集合中的点数,n2表示第二个集合中的点数
    int h[N], e[M], ne[M], idx;     // 邻接表存储所有边,匈牙利算法中只会用到从第一个集合指向第二个集合的边,所以这里只用存一个方向的边
    int match[N];       // 存储第二个集合中的每个点当前匹配的第一个集合中的点是哪个
    bool st[N];     // 表示第二个集合中的每个点是否已经被遍历过
    
    bool find(int x)
    {
        for (int i = h[x]; i != -1; i = ne[i])
        {
            int j = e[i];
            if (!st[j])
            {
                st[j] = true;
                if (match[j] == 0 || find(match[j]))
                {
                    match[j] = x;
                    return true;
                }
            }
        }
    
        return false;
    }
    
    // 求最大匹配数,依次枚举第一个集合中的每个点能否匹配第二个集合中的点
    int res = 0;
    for (int i = 1; i <= n1; i ++ )
    {
        memset(st, false, sizeof st);
        if (find(i)) res ++ ;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    3 工程化

    题目1:求二分图的最大匹配。

    #include 
    #include 
    #include 
    
    using namespace std;
    
    const int N = 510;
    int n1, n2, m;
    vector<vector<int>> g(N);
    int match[N];
    bool st[N];
    
    bool find(int a) {
        for (auto b : g[a]) {
            if (!st[b]) {
                st[b] = true;
                if (match[b] == 0 || find(match[b])) {
                    match[b] = a;
                    return true;
                }
            }
        }
        return false;
    }
    
    int main() {
        cin >> n1 >> n2 >> m;
        int a, b;
        while (m--) {
            cin >> a >> b;
            g[a].emplace_back(b);
        }
        
        int res = 0;
        for (int i = 1; i <= n1; ++i) {
            memset(st, 0, sizeof st);
            if (find(i)) res++;
        }
        
        cout << res << endl;
        
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
  • 相关阅读:
    Redis -- SpringBoot集成Redis
    实现HBase表和RDB表的转化(附Java源码资源)
    拷贝控制操作
    Arduino与Proteus仿真-Nokia5110 LCD界面菜单仿真
    利用亚马逊 云服务器 EC2 和S3免费套餐搭建私人网盘
    我赌你不懂系列:啥是序列化
    YoloV8改进策略:Diverse Branch Block改进YoloV8,继续在重参数结构上恐龙抗狼
    (附源码)计算机毕业设计JavaJava毕设项目补课管理系统
    (算法设计与分析)第三章动态规划-第一节2:动态规划之使用“斐波那契数列”问题说明重叠子问题如何解决
    C语言深入理解指针(非常详细)(二)
  • 原文地址:https://blog.csdn.net/YMWM_/article/details/134360071