test/yj_lca_squaring.test.cpp

Back to top page

Depends on

Code

Bundle
Copy
#define PROBLEM "https://judge.yosupo.jp/problem/lca"

#include <cstdio>

#include "Graph/adjacency_list.cpp"
#include "Graph/lowest_common_ancestor_squaring.cpp"

int main() {
  size_t n, q;
  scanf("%zu %zu", &n, &q);

  adjacency_list<weighted_edge<int>, undirected_tag> g(n);
  for (size_t i = 1; i < n; ++i) {
    size_t p;
    scanf("%zu", &p);
    g.emplace(i, p, 1);
  }

  lowest_common_ancestor lca(g, 0);
  for (size_t i = 0; i < q; ++i) {
    size_t u, v;
    scanf("%zu %zu", &u, &v);
    printf("%zu\n", lca(u, v));
  }
}

Back to top page