二分图的最大匹配(匈牙利算法)

今天学习了适用于二分图最大匹配的匈牙利算法,再此进行记录。

题目

思路

这个算法的思路用一个非常形象的例子来描述就是红娘牵线,左边n1个点看成男生,右边n2个点看成女生,从男生角度出发(女生也可以),然后进行配对。遍历当前男生喜欢的女生,如果对方女生还没对象,那么暂时配对成功,代码中表示为\(match[j] = x\),然后轮到下一个男生进行配对,如果下一个男生也看上了已经配对的女生,然后就让已经配对好的男生尝试进行让步,让步成功意味着可以配对,否则则失败,就这样一直进行下去,记录最大匹配数。

注意st数组的含义以及每一轮需要重新赋值!

代码

#include<bits/stdc++.h>

#define xx first
#define yy second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const bool MODE = 1;
const int INF = 0x3f3f3f3f, N = 510, M = 1e5 + 10;
int h[N], e[M], ne[M], idx;
int n1, n2, m;
int match[N];
bool st[N];  // st数组的含义:代表每一轮模拟中右边某个点是否已经被左边某个点预定了,这样左边的其他点就不会考虑右边这个点,转而考虑其他点了

inline void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

bool find(int x)
{
    for (int i = h[x]; ~i; i = ne[i])
    {
        int j = e[i];
        if (!st[j])
        {
            st[j] = true;
            if (!match[j] || find(match[j]))
            {
                match[j] = x;
                return true;
            }
        }

    }
    return false;
}

void solve()
{
    memset(h, -1, sizeof h);
    cin >> n1 >> n2 >> m;
    for (int i = 0; i < m; ++i)
    {
        int u, v;
        cin >> u >> v;
        add(u, v);
    }
    int res = 0;
    for (int i = 1; i <= n1; ++i)
    {
        memset(st, 0, sizeof st);  // 每一次加入新的点都是一轮模拟过程,所以st要重新赋值
        if (find(i))
            res++;
    }
    cout << res << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    if (MODE) freopen("Debug/input.txt", "r", stdin), freopen("Debug/output.txt", "w", stdout);
    solve();
    if (MODE) fclose(stdin), fclose(stdout);
    return 0;
}
posted @ 2023-02-09 19:31  Pluto_Evans  阅读(33)  评论(0)    收藏  举报