This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/lca"
#include "../misc/macros.h"
#include "../ds/HLD.h"
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<i64> a(n);
vector<vi> g(n);
for (int i = 1; i < n; ++i) {
int p;
cin >> p;
g[p].eb(i), g[i].eb(p);
}
auto hld = HLD(g);
while (q--) {
int u, v;
cin >> u >> v;
cout << hld.lca(u, v) << '\n';
}
}#line 1 "tests/LCA.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/lca"
#line 1 "misc/macros.h"
// #pragma GCC optimize("Ofast,unroll-loops") // unroll long, simple loops
// #pragma GCC target("avx2,fma") // vectorizing code
// #pragma GCC target("lzcnt,popcnt,abm,bmi,bmi2") // for fast bitset operation
#include <bits/extc++.h>
#include <tr2/dynamic_bitset>
using namespace std;
using namespace __gnu_pbds; // ordered_set, gp_hash_table
// using namespace __gnu_cxx; // rope
// for templates to work
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
#define pb push_back
#define eb emplace_back
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using ld = long double;
using pii = pair<i32, i32>;
using vi = vector<i32>;
// fast map
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash { // customize hash function for gp_hash_table
int operator()(int x) const { return x ^ RANDOM; }
};
gp_hash_table<int, int, chash> table;
/* ordered set
find_by_order(k): returns an iterator to the k-th element (0-based)
order_of_key(k): returns the number of elements in the set that are strictly less than k
*/
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
/* rope
rope <int> cur = v.substr(l, r - l + 1);
v.erase(l, r - l + 1);
v.insert(v.mutable_begin(), cur);
*/
#line 1 "ds/HLD.h"
template <class G>
struct HLD {
const G& g;
int n, t = 0;
vi sub, dep, par, head, pos, heavy;
HLD(const G& g, int root = 0) : g(g), n(sz(g)), sub(n), dep(n), par(n), head(n), pos(n), heavy(n, -1) {
par[root] = -1;
dfs_sub(root);
dfs_hld(root, root);
}
void dfs_sub(int u) {
sub[u] = 1;
for (int v : g[u])
if (v != par[u]) {
dep[v] = dep[u] + 1, par[v] = u;
dfs_sub(v);
sub[u] += sub[v];
if (heavy[u] == -1 || sub[v] > sub[heavy[u]]) heavy[u] = v;
}
}
void dfs_hld(int u, int h) {
head[u] = h, pos[u] = ++t;
if (heavy[u] != -1) dfs_hld(heavy[u], h);
for (int v : g[u])
if (v != par[u] && v != heavy[u]) dfs_hld(v, v);
}
int idx(int u) const { return pos[u]; }
pii query_subtree(int u) { return {pos[u], pos[u] + sub[u] - 1}; }
vector<pii> query_path(int u, int v) {
vector<pii> l, r;
while (head[u] != head[v]) {
if (dep[head[u]] > dep[head[v]]) {
l.pb({pos[u], pos[head[u]]});
u = par[head[u]];
} else {
r.pb({pos[head[v]], pos[v]});
v = par[head[v]];
}
}
if (dep[u] > dep[v]) l.pb({pos[u], pos[v]});
else r.pb({pos[u], pos[v]});
reverse(all(r));
l.insert(l.end(), all(r));
return l;
}
int lca(int u, int v) {
for (; head[u] != head[v]; u = par[head[u]])
if (dep[head[u]] < dep[head[v]]) swap(u, v);
return dep[u] < dep[v] ? u : v;
}
};
#line 5 "tests/LCA.test.cpp"
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<i64> a(n);
vector<vi> g(n);
for (int i = 1; i < n; ++i) {
int p;
cin >> p;
g[p].eb(i), g[i].eb(p);
}
auto hld = HLD(g);
while (q--) {
int u, v;
cin >> u >> v;
cout << hld.lca(u, v) << '\n';
}
}