Fu_L's Library

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

View the Project on GitHub Fu-L/cp-library

:heavy_check_mark: LowestCommonAncestor
(src/tree/lowest_common_ancestor.hpp)

LowestCommonAncestor

$n$ 頂点の根付き木が与えられたとき,

を前計算 $O(n \log n)$ クエリ $O(\log n)$ でできるアルゴリズムです.

内部はダブリングで実装されています.

コンストラクタ

LowestCommonAncestor<T> tree(Graph<T> g, int root = 0)

制約

計算量

depth

int tree.depth(int v)

辺の重みが $1$ であると仮定したときの根付き木 tree における頂点 $v$ の深さを返します.

制約

計算量

cost

T tree.cost(int v)

根付き木 tree における根 root と頂点 $v$ の距離を返します.

制約

計算量

parent

int tree.parent(int v)

頂点 $v$ の親頂点のラベルを返します.

$v$ が根であるときは $-1$ を返します.

制約

計算量

la

int tree.la(int v, int x)

頂点 $v$ から根方向に $x$ 個進んだ頂点のラベルを返します.

$x$ が 頂点 $v$ の深さよりも大きいときは $-1$ を返します.

制約

計算量

lca

int tree.lca(int u, int v)

根付き木 tree における頂点 $u$ と $v$ の最小共通祖先を返します.

制約

計算量

dist

int tree.dist(int u, int v)

辺の重みが $1$ であると仮定したときの根付き木 tree における頂点 $u$ と $v$ の間の距離を返します.

制約

計算量

length

T tree.length(int u, int v)

根付き木 tree における頂点 $u$ と $v$ の間の距離を返します.

制約

計算量

Depends on

Verified with

Code

#pragma once
#include "../template/template.hpp"
#include "../graph/graph_template.hpp"
template <typename T>
struct LowestCommonAncestor {
    LowestCommonAncestor(const Graph<T>& g, const int root = 0) {
        assert(0 <= root and root < g.size());
        init(g, root);
    }
    int depth(const int v) const {
        assert(0 <= v and v < n);
        return dep[v];
    }
    T cost(const int v) const {
        assert(0 <= v and v < n);
        return co[v];
    }
    int parent(const int v) const {
        assert(0 <= v and v < n);
        return par[0][v];
    }
    int la(int v, int x) const {
        assert(0 <= v and v < n);
        assert(x >= 0);
        if(x > dep[v]) return -1;
        for(int i = 0; x > 0; ++i) {
            if(x & 1) v = par[i][v];
            x >>= 1;
        }
        return v;
    }
    int lca(int u, int v) const {
        assert(0 <= u and u < n and 0 <= v and v < n);
        if(dep[u] > dep[v]) swap(u, v);
        v = la(v, dep[v] - dep[u]);
        if(u == v) return u;
        for(int i = (int)par.size() - 1; i >= 0; --i) {
            if(par[i][u] != par[i][v]) {
                u = par[i][u];
                v = par[i][v];
            }
        }
        return par[0][u];
    }
    int dist(const int u, const int v) const {
        assert(0 <= u and u < n and 0 <= v and v < n);
        return dep[u] + dep[v] - 2 * dep[lca(u, v)];
    }
    T length(const int u, const int v) const {
        assert(0 <= u and u < n and 0 <= v and v < n);
        return co[u] + co[v] - 2 * co[lca(u, v)];
    }

   private:
    int n;
    vector<vector<int>> par;
    vector<int> dep;
    vector<T> co;
    void init(const Graph<T>& g, const int root = 0) {
        n = g.size();
        int h = 1;
        while((1 << h) < n) ++h;
        par.assign(h, vector<int>(n, -1));
        dep.assign(n, -1);
        co.assign(n, -1);
        dfs(g, root, -1, 0, 0);
        for(int i = 0; i + 1 < (int)par.size(); ++i) {
            for(int v = 0; v < n; ++v) {
                if(par[i][v] != -1) {
                    par[i + 1][v] = par[i][par[i][v]];
                }
            }
        }
    }
    void dfs(const Graph<T>& g, const int v, const int p, const int d, const T& c) {
        par[0][v] = p;
        dep[v] = d;
        co[v] = c;
        for(const Edge<T>& e : g[v]) {
            if(e.to == p) continue;
            dfs(g, e.to, v, d + 1, c + e.cost);
        }
    }
};
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
#line 3 "src/graph/graph_template.hpp"
template <typename T>
struct Edge {
    int from, to;
    T cost;
    int idx;
    Edge()
        : from(-1), to(-1), cost(-1), idx(-1) {}
    Edge(const int from, const int to, const T& cost = 1, const int idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}
    operator int() const {
        return to;
    }
};
template <typename T>
struct Graph {
    Graph(const int N)
        : n(N), es(0), g(N) {}
    int size() const {
        return n;
    }
    int edge_size() const {
        return es;
    }
    void add_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }
    void add_directed_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es++);
    }
    inline vector<Edge<T>>& operator[](const int& k) {
        assert(0 <= k and k < n);
        return g[k];
    }
    inline const vector<Edge<T>>& operator[](const int& k) const {
        assert(0 <= k and k < n);
        return g[k];
    }

   private:
    int n, es;
    vector<vector<Edge<T>>> g;
};
template <typename T>
using Edges = vector<Edge<T>>;
#line 4 "src/tree/lowest_common_ancestor.hpp"
template <typename T>
struct LowestCommonAncestor {
    LowestCommonAncestor(const Graph<T>& g, const int root = 0) {
        assert(0 <= root and root < g.size());
        init(g, root);
    }
    int depth(const int v) const {
        assert(0 <= v and v < n);
        return dep[v];
    }
    T cost(const int v) const {
        assert(0 <= v and v < n);
        return co[v];
    }
    int parent(const int v) const {
        assert(0 <= v and v < n);
        return par[0][v];
    }
    int la(int v, int x) const {
        assert(0 <= v and v < n);
        assert(x >= 0);
        if(x > dep[v]) return -1;
        for(int i = 0; x > 0; ++i) {
            if(x & 1) v = par[i][v];
            x >>= 1;
        }
        return v;
    }
    int lca(int u, int v) const {
        assert(0 <= u and u < n and 0 <= v and v < n);
        if(dep[u] > dep[v]) swap(u, v);
        v = la(v, dep[v] - dep[u]);
        if(u == v) return u;
        for(int i = (int)par.size() - 1; i >= 0; --i) {
            if(par[i][u] != par[i][v]) {
                u = par[i][u];
                v = par[i][v];
            }
        }
        return par[0][u];
    }
    int dist(const int u, const int v) const {
        assert(0 <= u and u < n and 0 <= v and v < n);
        return dep[u] + dep[v] - 2 * dep[lca(u, v)];
    }
    T length(const int u, const int v) const {
        assert(0 <= u and u < n and 0 <= v and v < n);
        return co[u] + co[v] - 2 * co[lca(u, v)];
    }

   private:
    int n;
    vector<vector<int>> par;
    vector<int> dep;
    vector<T> co;
    void init(const Graph<T>& g, const int root = 0) {
        n = g.size();
        int h = 1;
        while((1 << h) < n) ++h;
        par.assign(h, vector<int>(n, -1));
        dep.assign(n, -1);
        co.assign(n, -1);
        dfs(g, root, -1, 0, 0);
        for(int i = 0; i + 1 < (int)par.size(); ++i) {
            for(int v = 0; v < n; ++v) {
                if(par[i][v] != -1) {
                    par[i + 1][v] = par[i][par[i][v]];
                }
            }
        }
    }
    void dfs(const Graph<T>& g, const int v, const int p, const int d, const T& c) {
        par[0][v] = p;
        dep[v] = d;
        co[v] = c;
        for(const Edge<T>& e : g[v]) {
            if(e.to == p) continue;
            dfs(g, e.to, v, d + 1, c + e.cost);
        }
    }
};
Back to top page