Luzhiled's Library

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

View the Project on GitHub ei1333/library

:heavy_check_mark: test/verify/aoj-grl-5-c-4.test.cpp

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_5_C"

#include "../../template/template.hpp"

#include "../../graph/tree/pmormq-lowest-common-ancestor.hpp"

int main() {
  int N, Q;
  cin >> N;
  PMORMQLowestCommonAncestor< int > dlca(N);
  for(int i = 0; i < N; i++) {
    int k;
    cin >> k;
    for(int j = 0; j < k; j++) {
      int c;
      cin >> c;
      dlca.add_edge(i, c);
    }
  }
  dlca.build();
  cin >> Q;
  for(int i = 0; i < Q; i++) {
    int u, v;
    cin >> u >> v;
    cout << dlca.lca(u, v) << "\n";
  }
}
#line 1 "test/verify/aoj-grl-5-c-4.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_5_C"

#line 1 "template/template.hpp"
#include<bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 1e9 + 7;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  explicit FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};
 
template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}
#line 4 "test/verify/aoj-grl-5-c-4.test.cpp"

#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);
      }
    }
  }
};
#line 6 "test/verify/aoj-grl-5-c-4.test.cpp"

int main() {
  int N, Q;
  cin >> N;
  PMORMQLowestCommonAncestor< int > dlca(N);
  for(int i = 0; i < N; i++) {
    int k;
    cin >> k;
    for(int j = 0; j < k; j++) {
      int c;
      cin >> c;
      dlca.add_edge(i, c);
    }
  }
  dlca.build();
  cin >> Q;
  for(int i = 0; i < Q; i++) {
    int u, v;
    cin >> u >> v;
    cout << dlca.lca(u, v) << "\n";
  }
}
Back to top page