【模板】带花树

学习博客


那些年我用带花树做过的题目:


带花树板子

#include <bits/stdc++.h>

using namespace std;
const int N = 1200 + 10;
int n, m, k;

inline int read() {
    register int x = 0, t = 1;
    register char ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-')ch = getchar();
    if (ch == '-')t = -1, ch = getchar();
    while (ch <= '9' && ch >= '0')x = x * 10 + ch - 48, ch = getchar();
    return x * t;
}

namespace FlowerTree { // 带花树 一般图最大匹配 O(FlowerTree_n^3)
    int FlowerTree_n;

    // FlowerTree_n 1000
    // 每个节点只能有一个匹配对象
    struct Edge {
        int to, next;
    } e[(300 * 600) * 6 + 10];
    int head[N], tot = 1;

    int fa[N];//fa[i] 记录i点属于哪一个点为根的花
    int pre[N];//pre[i] i的前驱节点
    queue<int> Q;//int Q[N * N * 2], h, t;//bfs队列 头尾
    int Match[N];//Match[i]标记匹配的对象
    int tim;//lca时间戳
    int Type[N];//Type[i]标记i点的类型 奇节点:1 偶节点:2 没有标记过时为0
    int dfn[N];//每个节点的lca时间戳


    void addEdge(int u, int v) { //建双边
        e[++tot] = {v, head[u]};
        head[u] = tot;
        e[++tot] = {u, head[v]};
        head[v] = tot;
    }

    int find(int x) {
        return fa[x] == x ? x : fa[x] = find(fa[x]);
    }

    int lca(int x, int y) {
       ++tim;
        x = find(x);
        y = find(y);
        while (dfn[x] != tim) {
            dfn[x] = tim;
            x = find(pre[Match[x]]);
            if (y) swap(x, y);
        }
        return x;
    }

    // 开花操作
    // 将奇环缩成一个点 并将原来奇点的点变为偶点 并加入队列中
    void blossom(int x, int y, int p) {
        while (find(x) != p) {
            pre[x] = y;
            y = Match[x];
            if (Type[y] == 2) {
                Type[y] = 1;
                Q.push(y);
            }
            fa[x] = p;
            fa[y] = p;
            x = pre[y];
        }
    }

    bool Aug(int st) { //从没有匹配的u点开始bfs找增广路 将其标记为A类点
        memset(Type, 0, sizeof Type);
        memset(pre, 0, sizeof pre);

        for (int i = 1; i <= FlowerTree_n; i++) {
            fa[i] = i;
        }

        while (!Q.empty())Q.pop();

        //------------------

        Q.push(st);
        Type[st] = 1;//标记为奇点

        while (!Q.empty()) {
            int u = Q.front();
            Q.pop();

            for (int i = head[u]; i; i = e[i].next) {
                int v = e[i].to;
                //如果遇到了偶节点 就忽略不管
                if (find(u) == find(v) || Type[v] == 2) continue;

                // 如果这个点没有被匹配过 说明找到了一条增广路
                // 因为队列里存的都是奇点 所以找到的那个没有匹配过的点肯定是偶点
                if (!Type[v]) {
                    Type[v] = 2;
                    pre[v] = u;

                    if (!Match[v]) {
                        // 找到now之前的那个节点x 将原先匹配过的x匹配现在的节点
                        // 让原来匹配的那个节点重新找别人匹配
                        for (int now = v, x; now; now = x) {
                            x = Match[pre[now]];
                            Match[now] = pre[now];
                            Match[pre[now]] = now;
                        }
                        return true;
                    }

                    //如果有匹配 则v是偶点 将v匹配的对象放入队列中
                    Type[Match[v]] = 1;
                    //Q[t++] = Match[v];
                    Q.push(Match[v]);
                } else if (Type[v] == 1) {
                    // 如果找到的点是奇点 即 找到环了
                    // 就进行开花操作
                    int p = lca(u, v);
                    blossom(u, v, p);
                    blossom(v, u, p);

                }
            }
        }
        return false;
    }

    void init() {
        memset(Match, 0, sizeof Match);
        memset(head, 0, sizeof head);
        memset(dfn, 0, sizeof dfn);

        tim = 0;
        tot = 1;
    }

}
using namespace FlowerTree;


int main() {
    ios::sync_with_stdio(false);

    int T;
    T = read();
    for (int cs = 1; cs <= T; cs++) {
        init();
        n = read();
        m = read();
        FlowerTree_n = n + n + m;
        for (int i = 1; i <= n; i++) {
            k = read();
            addEdge(i, i + n);
            for (int j = 1; j <= k; j++) {
                int v = read();
                addEdge(i, n + n + v);
                addEdge(i + n, n + n + v);
            }
        }

        int res = 0;
        for (int i = 1; i <= FlowerTree_n; i++) {
            if (!Match[i]) {
                res += Aug(i);
            }
        }

        printf("%d\n", res - n);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值