library2

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub goodstudyqaq/library2

:heavy_check_mark: graph/tree/union-find-lowest-common-ancestor.hpp

Depends on

Verified with

Code

#include "../../structure/union-find/union-find.hpp"
#include "../graph-template.hpp"
/*
并查集:
遍历完结点 u 的子树后, 将 u 和其父亲所在集合进行合并.
遍历到结点 u 时, 对所有询问 u,v, 若 v 已经被遍历过, 则结 果为 v 所在集合深度最小的结点.
复杂度 O((n + q)α(n)).
离线做法
*/

template <typename T = int>
struct UnionFindLowestCommonAncestor : Graph<T> {
    using Graph<T>::g;
    using Graph<T>::Graph;

    vector<int> build(vector<pair<int, int>> queries, int root = 0) {
        int n = g.size();
        vector<vector<pair<int, int>>> V(n);
        for (int i = 0; i < queries.size(); i++) {
            auto it = queries[i];
            V[it.first].push_back({it.second, i});
            V[it.second].push_back({it.first, i});
        }

        int m = queries.size();
        vector<int> vis(n);
        vector<int> ans(m);
        UnionFind uf(n);
        function<void(int, int)> dfs = [&](int u, int pre) {
            vis[u] = 1;
            for (auto it : V[u]) {
                int v = it.first;
                if (vis[v]) {
                    ans[it.second] = uf.find(v);
                }
            }
            for (auto v : g[u]) {
                if (v == pre) continue;
                dfs(v, u);
            }

            if (pre != -1) {
                uf.unite(u, pre);
            }
        };
        dfs(root, -1);
        return ans;
    }
};
#line 2 "structure/union-find/union-find.hpp"
#include <bits/stdc++.h>

using namespace std;

struct UnionFind {
    vector<int> data;
    vector<int> f;

    UnionFind() = default;

    explicit UnionFind(size_t sz) : data(sz, 1), f(sz) {
        iota(f.begin(), f.end(), 0);
    }

    bool unite(int x, int y) {  // x merge to y
        x = find(x), y = find(y);
        if (x == y) return false;
        data[y] += data[x];
        f[x] = y;
        return true;
    }
    int find(int x) {
        if (f[x] == x) return x;
        int y = find(f[x]);
        data[y] += data[x];
        f[x] = y;
        return f[x];
    }
    int size(int x) {
        return data[find(x)];
    }
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    vector<vector<int>> groups() {
        int n = (int)data.size();
        vector<vector<int>> ans(n);
        for (int i = 0; i < n; i++) {
            ans[find(i)].push_back(i);
        }

        ans.erase(remove_if(ans.begin(), ans.end(), [&](const vector<int>& v) {
                      return v.empty();
                  }),
                  ans.end());
        return ans;
    }
};
#line 3 "graph/graph-template.hpp"
using namespace std;

template <typename T = int>
struct Edge {
    int from, to;
    T cost;
    int idx;

    Edge() = default;

    Edge(int from, int to, T cost = 1, int idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}

    operator int() const { return to; }
};

template <typename T = int>
struct Graph {
    vector<vector<Edge<T> > > g;
    int es;

    Graph() = default;

    explicit Graph(int n) : g(n), es(0) {}

    size_t size() const { return g.size(); }

    virtual void add_directed_edge(int from, int to, T cost = 1) {
        g[from].emplace_back(from, to, cost, es++);
    }

    // virtual 可以被重载,实现多态
    virtual void add_edge(int from, int to, T cost = 1) {
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }

    void read(int M, int padding = -1, bool weighted = false,
              bool directed = false) {
        for (int i = 0; i < M; i++) {
            int a, b;
            cin >> a >> b;
            a += padding;
            b += padding;
            T c = T(1);
            if (weighted) cin >> c;
            if (directed)
                add_directed_edge(a, b, c);
            else
                add_edge(a, b, c);
        }
    }

    inline vector<Edge<T> > &operator[](const int &k) { return g[k]; }

    inline const vector<Edge<T> > &operator[](const int &k) const { return g[k]; }
};

template <typename T = int>
using Edges = vector<Edge<T> >;
#line 3 "graph/tree/union-find-lowest-common-ancestor.hpp"
/*
并查集:
遍历完结点 u 的子树后, 将 u 和其父亲所在集合进行合并.
遍历到结点 u 时, 对所有询问 u,v, 若 v 已经被遍历过, 则结 果为 v 所在集合深度最小的结点.
复杂度 O((n + q)α(n)).
离线做法
*/

template <typename T = int>
struct UnionFindLowestCommonAncestor : Graph<T> {
    using Graph<T>::g;
    using Graph<T>::Graph;

    vector<int> build(vector<pair<int, int>> queries, int root = 0) {
        int n = g.size();
        vector<vector<pair<int, int>>> V(n);
        for (int i = 0; i < queries.size(); i++) {
            auto it = queries[i];
            V[it.first].push_back({it.second, i});
            V[it.second].push_back({it.first, i});
        }

        int m = queries.size();
        vector<int> vis(n);
        vector<int> ans(m);
        UnionFind uf(n);
        function<void(int, int)> dfs = [&](int u, int pre) {
            vis[u] = 1;
            for (auto it : V[u]) {
                int v = it.first;
                if (vis[v]) {
                    ans[it.second] = uf.find(v);
                }
            }
            for (auto v : g[u]) {
                if (v == pre) continue;
                dfs(v, u);
            }

            if (pre != -1) {
                uf.unite(u, pre);
            }
        };
        dfs(root, -1);
        return ans;
    }
};
Back to top page