library_cpp

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub hidehic0/library_cpp

:heavy_check_mark: verify/library_checker_lca_hld.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/lca

#include <bits/stdc++.h>
using namespace std;

#include "templates/alias.hpp"
#include "templates/macro.hpp"

#include "tree/heavy_light_decomposition.hpp"

int main() {
  ll N, Q;
  in(N, Q);
  vvi G(N);

  vi P(N - 1);
  in(P);

  rep(i, N - 1) G[P[i]].emplace_back(i + 1);

  HeavyLightDecomposition<ll> hld(G);

  while (Q--) {
    ll u, v;
    in(u, v);

    out(hld.lca(u, v));
  }
}
#line 1 "verify/library_checker_lca_hld.cpp"
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/lca

#include <bits/stdc++.h>
using namespace std;

#line 7 "templates/alias.hpp"

template <class T> using VC = std::vector<T>;
template <class T>
using rpriority_queue = std::priority_queue<T, VC<T>, std::greater<T>>;
using ll = long long;
using ld = long double;
using pii = std::pair<ll, ll>;
using vi = VC<ll>;
using vvi = VC<vi>;
using vvvi = VC<vvi>;
using vb = VC<bool>;
using vvb = VC<vb>;
using vf = VC<double>;
using vvf = VC<vf>;
using vpii = VC<pii>;
using vvpii = VC<vpii>;
using si = std::set<ll>;
using spii = std::set<pii>;
using mii = std::map<ll, ll>;
const std::string upperlist = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const std::string lowerlist = "abcdefghijklmnopqrstuvwxyz";
#define mp make_pair
#define dms << " " <<

constexpr int MOD998 = 998244353;
#line 4 "templates/macro.hpp"

// 引数の長さで内容が変わるrep 参考: https://trap.jp/post/1224
#define overload4(a, b, c, d, ...) d
#define _rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) overload4(__VA_ARGS__, REP, _rep)(__VA_ARGS__)
#define _rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define RREP(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); i--)
#define rrep(...) overload4(__VA_ARGS__, RREP, _rrep)(__VA_ARGS__)

#define all(a) (a).begin(), (a).end()

template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

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

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

// pythonのprintライクな関数 参考:
// https://nyaannyaan.github.io/library/template/inout.hpp
inline void out() { std::cout << std::endl; }
template <typename T, typename... U, char sep = ' '>
void out(const T &t, const U &...u) {
  std::cout << t;

  if (sizeof...(u))
    std::cout << sep;

  out(u...);
}

// cinの短縮関数 参考: https://nyaannyaan.github.io/library/template/inout.hpp
inline void in() {}
template <typename T, class... U> void in(T &t, U &...u) {
  std::cin >> t;
  in(u...);
}

template <typename T> inline T ceil_div(T a, T b) { return (a + b - 1) / b; }
template <typename T> inline T mod_pow(T a, T n, T mod) {
  T res = 1;
  while (n) {
    if (n % 2 != 0) {
      res *= a;
      res %= mod;
    }

    a *= a;
    a %= mod;

    n >>= 1;
  }

  return res;
}
template <typename T> inline T minus_mod(T a, T b) { return ((a % b) + b) % b; }

template <typename T> void apply_vec(std::vector<T> &v, T (*fn)(T)) {
  for (int i = 0; i < v.size(); i++)
    v[i] = fn(v[i]);
}
#line 8 "verify/library_checker_lca_hld.cpp"

#line 3 "tree/heavy_light_decomposition.hpp"

template <std::integral T> struct HeavyLightDecomposition {
public:
  std::vector<std::vector<T>> G;
  T n;
  std::vector<T> size, par, in, out, head, depth, rev;

  HeavyLightDecomposition(std::vector<std::vector<T>> G, T root = 0)
      : G{G}, n(static_cast<T>(G.size())), size(n, 0), par(n, -1), in(n, 0),
        out(n, 0), head(n, root), depth(n, 0), rev(n, -1), d{0}, o{0} {

    dfs_sz(root);
    dfs_hld(root);
  }

  T lca(T u, T v) {
    assert(0 <= u && u < n && 0 <= v && v < n);

    for (;; u = par[head[u]]) {
      if (in[u] < in[v])
        std::swap(u, v);

      if (head[u] == head[v])
        return v;
    }
  }

  template <typename S>
  S query(T u, T v, const auto &e, const auto &q, const auto &op,
          bool edge = false) {
    assert(0 <= u && u < n && 0 <= v && v < n);

    static_assert(std::is_convertible_v<decltype(op), std::function<S(S, S)>>,
                  "op must work as S(S, S)");
    static_assert(std::is_convertible_v<decltype(e), std::function<S()>>,
                  "e must work as S()");
    static_assert(std::is_convertible_v<decltype(q), std::function<S(T, T)>>,
                  "q must work as S(T, T)");

    S l = e(), r = e();

    for (;; u = par[head[u]]) {
      if (in[u] < in[v])
        std::swap(u, v), std::swap(l, r);
      if (head[u] == head[v])
        break;

      l = op(q(in[head[u]], in[u] + 1), l);
    }

    return op(op(q(in[v] + edge, in[u] + 1), l), r);
  }

private:
  T d, o;

  void dfs_sz(T cur) {
    size[cur] = 1;

    if (G[cur].size() >= 2 && G[cur][0] == par[cur])
      swap(G[cur][0], G[cur][1]);

    for (auto &nxt : G[cur]) {
      if (par[cur] == nxt)
        continue;

      par[nxt] = cur;
      dfs_sz(nxt);
      size[cur] += size[nxt];

      if (size[G[cur][0]] < size[nxt])
        swap(G[cur][0], nxt);
    }
  }

  void dfs_hld(T cur) {
    in[cur] = o++, rev[in[cur]] = cur;
    depth[cur] = d;

    for (auto nxt : G[cur]) {
      if (nxt == par[cur])
        continue;

      head[nxt] = (nxt == G[cur][0] ? head[cur] : nxt);
      ++d, dfs_hld(nxt), --d;
    }

    out[cur] = o;
  }
};

/**
 * @file tree/heavy_light_decomposition.hpp
 * @brief HeavyLightDecomposition
 * @auther hidehic0
 * @date 2026-05-31
 */
#line 10 "verify/library_checker_lca_hld.cpp"

int main() {
  ll N, Q;
  in(N, Q);
  vvi G(N);

  vi P(N - 1);
  in(P);

  rep(i, N - 1) G[P[i]].emplace_back(i + 1);

  HeavyLightDecomposition<ll> hld(G);

  while (Q--) {
    ll u, v;
    in(u, v);

    out(hld.lca(u, v));
  }
}

Test cases

Env Name Status Elapsed Memory
g++ almost_line_00 :heavy_check_mark: AC 1000 ms 114 MB
g++ almost_line_01 :heavy_check_mark: AC 917 ms 114 MB
g++ binary_00 :heavy_check_mark: AC 1232 ms 93 MB
g++ binary_01 :heavy_check_mark: AC 1917 ms 93 MB
g++ binary_02 :heavy_check_mark: AC 1158 ms 93 MB
g++ example_00 :heavy_check_mark: AC 2 ms 4 MB
g++ line_00 :heavy_check_mark: AC 1075 ms 113 MB
g++ line_01 :heavy_check_mark: AC 702 ms 134 MB
g++ line_02 :heavy_check_mark: AC 585 ms 18 MB
g++ line_03 :heavy_check_mark: AC 226 ms 124 MB
g++ line_04 :heavy_check_mark: AC 280 ms 81 MB
g++ max_line_00 :heavy_check_mark: AC 1242 ms 144 MB
g++ max_line_01 :heavy_check_mark: AC 860 ms 144 MB
g++ max_line_02 :heavy_check_mark: AC 844 ms 144 MB
g++ max_random_00 :heavy_check_mark: AC 991 ms 96 MB
g++ max_random_01 :heavy_check_mark: AC 1211 ms 96 MB
g++ max_random_02 :heavy_check_mark: AC 1169 ms 96 MB
g++ path_graph_root_centroid_00 :heavy_check_mark: AC 1203 ms 130 MB
g++ path_graph_root_centroid_01 :heavy_check_mark: AC 1240 ms 130 MB
g++ path_graph_root_centroid_02 :heavy_check_mark: AC 868 ms 130 MB
g++ random_00 :heavy_check_mark: AC 814 ms 75 MB
g++ random_01 :heavy_check_mark: AC 1076 ms 89 MB
g++ random_02 :heavy_check_mark: AC 558 ms 13 MB
g++ random_03 :heavy_check_mark: AC 289 ms 83 MB
g++ random_04 :heavy_check_mark: AC 270 ms 54 MB
Back to top page