解决一般图最大匹配——带花树算法

先看前面的,再看后面的。

https://www.cnblogs.com/zhoushuyu/p/8717234.html

https://www.cnblogs.com/LUO257316/p/3277871.html

实现的思路已经懂了。

但是代码还没搞。

// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #pragma GCC optimize(4)
#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>
#include <unordered_set>
// #include<bits/extc++.h>
// using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define fi first
#define se second
#define debug(x) cerr<<#x << " := " << x << endl;
#define bug cerr<<"-----------------------"<<endl;
#define FOR(a, b, c) for(int a = b; a <= c; ++ a)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;

template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}

/**********showtime************/
const int N = 509;
int belong[N];
int findb(int x) {
    return belong[x] == x ? x : belong[x] = findb(belong[x]);
}
void unit(int a, int b) {
    a = findb(a);
    b = findb(b);
    if (a != b) belong[a] = b;
}

int n, match[N], m;
vector<int> e[N];
int Q[N], rear;
int nx[N], mark[N], vis[N];

// 朴素算法求某阶段中搜索树上两点x, y的最近公共祖先r
int LCA(int x, int y) {
    static int t = 0; t++;
    while (true) {
        if (x != -1) {
            x = findb(x); // 点要对应到对应的花上去
            if (vis[x] == t) return x;
            vis[x] = t;
            if (match[x] != -1) x = nx[match[x]];
            else x = -1;
        }
        swap(x, y);
    }
}

void group(int a, int p) {
    while (a != p) {
        int b = match[a], c = nx[b];

        // next数组是用来标记花朵中的路径的,综合match数组来用,实际上形成了
        // 双向链表,如(x, y)是匹配的,next[x]和next[y]就可以指两个方向了。
        if (findb(c) != p) nx[c] = b;

        // 奇环中的点都有机会向环外找到匹配,所以都要标记成S型点加到队列中去,
        // 因环内的匹配数已饱和,因此这些点最多只允许匹配成功一个点,在aug中
        // 每次匹配到一个点就break终止了当前阶段的搜索,并且下阶段的标记是重
        // 新来过的,这样做就是为了保证这一点。
        if (mark[b] == 2) mark[Q[rear++] = b] = 1;
        if (mark[c] == 2) mark[Q[rear++] = c] = 1;

        unit(a, b); unit(b, c);
        a = c;
    }
}

// 增广
void aug(int s) {
    for (int i = 0; i < n; i++) // 每个阶段都要重新标记
        nx[i] = -1, belong[i] = i, mark[i] = 0, vis[i] = -1;
    mark[s] = 1;
    Q[0] = s; rear = 1;
    for (int front = 0; match[s] == -1 && front < rear; front++) {
        int x = Q[front]; // 队列Q中的点都是S型的
        for (int i = 0; i < (int)e[x].size(); i++) {
            int y = e[x][i];
            if (match[x] == y) continue; // x与y已匹配,忽略
            if (findb(x) == findb(y)) continue; // x与y同在一朵花,忽略
            if (mark[y] == 2) continue; // y是T型点,忽略
            if (mark[y] == 1) { // y是S型点,奇环缩点
                int r = LCA(x, y); // r为从i和j到s的路径上的第一个公共节点
                if (findb(x) != r) nx[x] = y; // r和x不在同一个花朵,next标记花朵内路径
                if (findb(y) != r) nx[y] = x; // r和y不在同一个花朵,next标记花朵内路径

                // 将整个r -- x - y --- r的奇环缩成点,r作为这个环的标记节点,相当于论文中的超级节点
                group(x, r); // 缩路径r --- x为点
                group(y, r); // 缩路径r --- y为点
            }
            else if (match[y] == -1) { // y自由,可以增广,R12规则处理
                nx[y] = x;
                for (int u = y; u != -1; ) { // 交叉链取反
                    int v = nx[u];
                    int mv = match[v];
                    match[v] = u, match[u] = v;
                    u = mv;
                }
                break; // 搜索成功,退出循环将进入下一阶段
            }
            else { // 当前搜索的交叉链+y+match[y]形成新的交叉链,将match[y]加入队列作为待搜节点
                nx[y] = x;
                mark[Q[rear++] = match[y]] = 1; // match[y]也是S型的
                mark[y] = 2; // y标记成T型
            }
        }
    }
}

bool g[N][N];
int main() {
    scanf("%d%d", &n,&m);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) g[i][j] = false;

    // 建图,双向边
    int x, y;
    for(int i = 1; i <= m; ++i){
        scanf("%d%d", &x, &y);
        x--, y--;
        if (x != y && !g[x][y])
            e[x].push_back(y), e[y].push_back(x);
        g[x][y] = g[y][x] = true;
    }

    // 增广匹配
    for (int i = 0; i < n; i++) match[i] = -1;
    for (int i = 0; i < n; i++) if (match[i] == -1) aug(i);

    // 输出答案
    int tot = 0;
    for (int i = 0; i < n; i++) if (match[i] != -1) tot++;
    printf("%d
", tot/2);
    for (int i = 0; i < n; i++)
        printf("%d ", match[i] + 1);
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/ckxkexing/p/11259475.html