Luzhiled's Library

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

View the Project on GitHub ei1333/library

:question: PMORMQ-Lowest-Common-Ancestor(最小共通祖先)
(graph/tree/pmormq-lowest-common-ancestor.hpp)

概要

オイラーツアーと±1RMQ によって最小共通祖先を求める.

辺属性のオイラーツアーをする. すべての頂点について, その頂点 $k$ に最初に到達した時刻 $in[k]$ と深さ $dep[k]$ を求めておく. 頂点 $u, v$ の最小共通祖先は区間 $[in[u], in[v]]$ の要素のうち深さが最小となる頂点である.

スパーステーブルを用いると前計算 $O(V \log V)$, クエリ $O(1)$ となるが, $dep[k]$ 隣接する要素の差がちょうど $1$ であることを利用した ±1RMQ を用いると前計算 $O(V)$, クエリ $O(1)$ となる.

使い方

計算量

Depends on

Verified with

Code

#pragma once

#include "../graph-template.hpp"
#include "../../structure/others/plus-minus-one-rmq.hpp"

/**
 * @brief PMORMQ-Lowest-Common-Ancestor(最小共通祖先)
 * @docs docs/pmormq-lowest-common-ancestor.md
 **/
template< typename T = int >
struct PMORMQLowestCommonAncestor : Graph< T > {
public:
  using Graph< T >::Graph;
  using Graph< T >::g;
  using F = function< int(int, int) >;

  void build(int root = 0) {
    ord.reserve(g.size() * 2 - 1);
    dep.reserve(g.size() * 2 - 1);
    in.resize(g.size());
    dfs(root, -1, 0);
    vector< int > vs(g.size() * 2 - 1);
    iota(begin(vs), end(vs), 0);
    st = PlusMinusOneRMQ< int >(dep);
  }

  int lca(int x, int y) const {
    if(in[x] > in[y]) swap(x, y);
    return ord[st.fold(in[x], in[y] + 1).second];
  }

private:
  vector< int > ord, dep, in;
  PlusMinusOneRMQ< int > st;

  void dfs(int idx, int par, int d) {
    in[idx] = (int) ord.size();
    ord.emplace_back(idx);
    dep.emplace_back(d);
    for(auto &to : g[idx]) {
      if(to != par) {
        dfs(to, idx, d + 1);
        ord.emplace_back(idx);
        dep.emplace_back(d);
      }
    }
  }
};
#line 2 "graph/tree/pmormq-lowest-common-ancestor.hpp"

#line 2 "graph/graph-template.hpp"

/**
 * @brief Graph Template(グラフテンプレート)
 */
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();
  }

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

  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 1 "structure/others/sparse-table.hpp"
/**
 * @brief Sparse-Table(スパーステーブル)
 * @docs docs/sparse-table.md
 */
template< typename T, typename F >
struct SparseTable {
  F f;
  vector< vector< T > > st;
  vector< int > lookup;

  SparseTable() = default;

  explicit SparseTable(const vector< T > &v, const F &f) : f(f) {
    const int n = (int) v.size();
    const int b = 32 - __builtin_clz(n);
    st.assign(b, vector< T >(n));
    for(int i = 0; i < v.size(); i++) {
      st[0][i] = v[i];
    }
    for(int i = 1; i < b; i++) {
      for(int j = 0; j + (1 << i) <= n; j++) {
        st[i][j] = f(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
      }
    }
    lookup.resize(v.size() + 1);
    for(int i = 2; i < lookup.size(); i++) {
      lookup[i] = lookup[i >> 1] + 1;
    }
  }

  inline T fold(int l, int r) const {
    int b = lookup[r - l];
    return f(st[b][l], st[b][r - (1 << b)]);
  }
};

template< typename T, typename F >
SparseTable< T, F > get_sparse_table(const vector< T > &v, const F &f) {
  return SparseTable< T, F >(v, f);
}
#line 2 "structure/others/plus-minus-one-rmq.hpp"

/**
 * @brief Plus-Minus-One-RMQ
 **/
template< typename T >
struct PlusMinusOneRMQ {
  using F = function< int(int, int) >;

  int backet;
  vector< T > vs;
  vector< int > bidx, bbit;
  SparseTable< int, F > st;
  vector< vector< vector< int > > > lookup;

  explicit PlusMinusOneRMQ() = default;

  explicit PlusMinusOneRMQ(const vector< T > &vs) : vs(vs) {
    int n = (int) vs.size();
    backet = max(1, (31 - __builtin_clz(n)) / 2);
    int sz = (n + backet - 1) / backet;
    bidx.assign(sz, -1);
    bbit.assign(sz, 0);
    for(int i = 0; i < sz; i++) {
      int l = i * backet;
      int r = min(l + backet, n);
      bidx[i] = l;
      for(int j = l + 1; j < r; j++) {
        if(vs[j] < vs[bidx[i]]) bidx[i] = j;
        if(vs[j - 1] < vs[j]) bbit[i] |= 1 << (j - l - 1);
      }
    }
    F f = [&](int a, int b) { return vs[a] < vs[b] ? a : b; };
    st = get_sparse_table(bidx, f);
    lookup.assign(1 << (backet - 1), vector< vector< int > >(backet, vector< int >(backet + 1)));
    for(int i = 0; i < (1 << (backet - 1)); i++) {
      for(int j = 0; j < backet; j++) {
        int sum = 0, ret = 0, pos = j;
        for(int k = j + 1; k <= backet; k++) {
          lookup[i][j][k] = pos;
          if(i & (1 << (k - 1))) ++sum;
          else --sum;
          if(sum < ret) {
            pos = k;
            ret = sum;
          }
        }
      }
    }
  }

  pair< T, int > fold(int l, int r) const {
    int lb = l / backet;
    int rb = r / backet;
    if(lb == rb) {
      int pos = lb * backet + lookup[bbit[lb]][l % backet][r % backet];
      return {vs[pos], pos};
    }
    int pos = lb * backet + lookup[bbit[lb]][l % backet][backet];
    if(r % backet > 0) {
      int sub = rb * backet + lookup[bbit[rb]][0][r % backet];
      if(vs[sub] < vs[pos]) pos = sub;
    }
    if(lb + 1 == rb) {
      return {vs[pos], pos};
    } else {
      int sub = st.fold(lb + 1, rb);
      if(vs[sub] < vs[pos]) pos = sub;
      return {vs[pos], pos};
    }
  }
};
#line 5 "graph/tree/pmormq-lowest-common-ancestor.hpp"

/**
 * @brief PMORMQ-Lowest-Common-Ancestor(最小共通祖先)
 * @docs docs/pmormq-lowest-common-ancestor.md
 **/
template< typename T = int >
struct PMORMQLowestCommonAncestor : Graph< T > {
public:
  using Graph< T >::Graph;
  using Graph< T >::g;
  using F = function< int(int, int) >;

  void build(int root = 0) {
    ord.reserve(g.size() * 2 - 1);
    dep.reserve(g.size() * 2 - 1);
    in.resize(g.size());
    dfs(root, -1, 0);
    vector< int > vs(g.size() * 2 - 1);
    iota(begin(vs), end(vs), 0);
    st = PlusMinusOneRMQ< int >(dep);
  }

  int lca(int x, int y) const {
    if(in[x] > in[y]) swap(x, y);
    return ord[st.fold(in[x], in[y] + 1).second];
  }

private:
  vector< int > ord, dep, in;
  PlusMinusOneRMQ< int > st;

  void dfs(int idx, int par, int d) {
    in[idx] = (int) ord.size();
    ord.emplace_back(idx);
    dep.emplace_back(d);
    for(auto &to : g[idx]) {
      if(to != par) {
        dfs(to, idx, d + 1);
        ord.emplace_back(idx);
        dep.emplace_back(d);
      }
    }
  }
};
Back to top page