Luzhiled's Library

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

View the Project on GitHub ei1333/library

:heavy_check_mark: Doubling-Lowest-Common-Ancestor(最小共通祖先)
(graph/tree/doubling-lowest-common-ancestor.hpp)

概要

ダブリングによって最小共通祖先を求める.

頂点 $u$, $v$ の最小共通祖先を求めたいとする. $dep[i]$ をある頂点を根とする根付き木としてみたときの深さとし, $dep[u] \leq dep[v]$ を仮定する. まず $dep[v] - dep[u]$ 個だけ頂点 $v$ を親に遡らせて, 頂点 $u, v$ の深さを揃える. このとき深さが一致したらそれが最小共通祖先. それ以外のとき, 上位 bit から頂点 $u, v$ 双方の $2^k$ 個先の親を見て, 異なれば遡ることを繰り返し, 双方の親ではない直前の頂点を求める. するとその親が最小共通祖先であることがわかる.

使い方

計算量

Depends on

Verified with

Code

#pragma once

#include "../graph-template.hpp"

/**
 * @brief Doubling-Lowest-Common-Ancestor(最小共通祖先)
 * @docs docs/doubling-lowest-common-ancestor.md
 */
template< typename T >
struct DoublingLowestCommonAncestor : Graph< T > {
public:
  using Graph< T >::g;
  vector< int > dep;
  vector< T > sum;
  vector< vector< int > > table;
  const int LOG;

  explicit DoublingLowestCommonAncestor(int n)
      : Graph< T >(n), LOG(32 - __builtin_clz(g.size())) {}

  explicit DoublingLowestCommonAncestor(const Graph< T > &g)
      : LOG(32 - __builtin_clz(g.size())), Graph< T >(g) {}

  void build(int root = 0) {
    dep.assign(g.size(), 0);
    sum.assign(g.size(), 0);
    table.assign(LOG, vector< int >(g.size(), -1));
    dfs(root, -1, 0);
    for(int k = 0; k + 1 < LOG; k++) {
      for(int i = 0; i < (int)table[k].size(); i++) {
        if(table[k][i] == -1) table[k + 1][i] = -1;
        else table[k + 1][i] = table[k][table[k][i]];
      }
    }
  }

  int lca(int u, int v) {
    if(dep[u] > dep[v]) swap(u, v);
    v = climb(v, dep[v] - dep[u]);
    if(u == v) return u;
    for(int i = LOG - 1; i >= 0; i--) {
      if(table[i][u] != table[i][v]) {
        u = table[i][u];
        v = table[i][v];
      }
    }
    return table[0][u];
  }

  int climb(int u, int k) {
    if(dep[u] < k) return -1;
    for(int i = LOG - 1; i >= 0; i--) {
      if((k >> i) & 1) u = table[i][u];
    }
    return u;
  }

  T dist(int u, int v) {
    return sum[u] + sum[v] - 2 * sum[lca(u, v)];
  }

private:
  void dfs(int idx, int par, int d) {
    table[0][idx] = par;
    dep[idx] = d;
    for(auto &to : g[idx]) {
      if(to != par) {
        sum[to] = sum[idx] + to.cost;
        dfs(to, idx, d + 1);
      }
    }
  }
};
#line 2 "graph/tree/doubling-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 4 "graph/tree/doubling-lowest-common-ancestor.hpp"

/**
 * @brief Doubling-Lowest-Common-Ancestor(最小共通祖先)
 * @docs docs/doubling-lowest-common-ancestor.md
 */
template< typename T >
struct DoublingLowestCommonAncestor : Graph< T > {
public:
  using Graph< T >::g;
  vector< int > dep;
  vector< T > sum;
  vector< vector< int > > table;
  const int LOG;

  explicit DoublingLowestCommonAncestor(int n)
      : Graph< T >(n), LOG(32 - __builtin_clz(g.size())) {}

  explicit DoublingLowestCommonAncestor(const Graph< T > &g)
      : LOG(32 - __builtin_clz(g.size())), Graph< T >(g) {}

  void build(int root = 0) {
    dep.assign(g.size(), 0);
    sum.assign(g.size(), 0);
    table.assign(LOG, vector< int >(g.size(), -1));
    dfs(root, -1, 0);
    for(int k = 0; k + 1 < LOG; k++) {
      for(int i = 0; i < (int)table[k].size(); i++) {
        if(table[k][i] == -1) table[k + 1][i] = -1;
        else table[k + 1][i] = table[k][table[k][i]];
      }
    }
  }

  int lca(int u, int v) {
    if(dep[u] > dep[v]) swap(u, v);
    v = climb(v, dep[v] - dep[u]);
    if(u == v) return u;
    for(int i = LOG - 1; i >= 0; i--) {
      if(table[i][u] != table[i][v]) {
        u = table[i][u];
        v = table[i][v];
      }
    }
    return table[0][u];
  }

  int climb(int u, int k) {
    if(dep[u] < k) return -1;
    for(int i = LOG - 1; i >= 0; i--) {
      if((k >> i) & 1) u = table[i][u];
    }
    return u;
  }

  T dist(int u, int v) {
    return sum[u] + sum[v] - 2 * sum[lca(u, v)];
  }

private:
  void dfs(int idx, int par, int d) {
    table[0][idx] = par;
    dep[idx] = d;
    for(auto &to : g[idx]) {
      if(to != par) {
        sum[to] = sum[idx] + to.cost;
        dfs(to, idx, d + 1);
      }
    }
  }
};
Back to top page