This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/lca"
#include "Template/template.hpp"
#include "Graph/hld.hpp"
int main(){
int n,q;
cin>>n>>q;
HLD hld(n);
rep(i,1,n){
int p;
cin>>p;
hld.add_edge(i,p);
}
hld.run();
while(q--){
int x,y;
cin>>x>>y;
cout<<hld.lca(x,y)<<'\n';
}
return 0;
}
#line 1 "Verify/LC_lca.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/lca"
#line 1 "Template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())
using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;
template <typename T, typename S = T> S SUM(const vector<T> &a) {
return accumulate(ALL(a), S(0));
}
template <typename S, typename T = S> S POW(S a, T b) {
S ret = 1, base = a;
for (;;) {
if (b & 1)
ret *= base;
b >>= 1;
if (b == 0)
break;
base *= base;
}
return ret;
}
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
assert(y != 0);
if (y < 0)
x = -x, y = -y;
return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
assert(y != 0);
if (y < 0)
x = -x, y = -y;
return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
return (x == 0 ? -1 : __builtin_ctzll(x));
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "P(" << p.first << ", " << p.second << ")";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "{";
for (int i = 0; i < vec.size(); i++) {
os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
}
os << "}";
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
os << "{";
for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
os << "(" << itr->first << ", " << itr->second << ")";
itr++;
if (itr != map_var.end())
os << ", ";
itr--;
}
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
os << "{";
for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
os << *itr;
++itr;
if (itr != set_var.end())
os << ", ";
itr--;
}
os << "}";
return os;
}
#ifdef LOCAL
#define debug 1
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define debug 0
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
for (; a[i] != ',' && a[i] != '\0'; i++)
cerr << a[i];
cerr << ":" << b << " ";
_show(i + 1, a, c...);
}
#line 2 "Graph/hld.hpp"
struct HLD{
using P=pair<int,int>;
vector<vector<int>> g; vector<int> sz,in,out,rev,hs,par,dist;
void dfs(int v,int p){
par[v]=p; sz[v]=1;
if(p!=-1)dist[v]=dist[p]+1;
if(!g[v].empty() and g[v][0]==p)swap(g[v][0],g[v].back());
for(auto& to:g[v])if(to!=p){
dfs(to,v); sz[v]+=sz[to];
if(sz[g[v][0]]<sz[to])swap(g[v][0],to);
}
}
void dfs2(int v,int p,int& k){
in[v]=k++; rev[in[v]]=v;
for(auto& to:g[v])if(to!=p){
hs[to]=(g[v][0]==to?hs[v]:to);
dfs2(to,v,k);
}
out[v]=k;
}
HLD(int _n):g(_n),sz(_n),in(_n),out(_n),rev(_n),hs(_n),par(_n),dist(_n){}
void add_edge(int u,int v){
g[u].emplace_back(v); g[v].emplace_back(u);
}
void run(int rt=0){dfs(rt,-1); hs[rt]=rt; int k=0; dfs2(rt,-1,k);}
int lca(int u,int v){
for(;;v=par[hs[v]]){
if(in[u]>in[v])swap(u,v);
if(hs[u]==hs[v])return u;
}
}
vector<P> get(int u,int p,bool es=0){
assert(in[p]<=in[u] and out[u]<=out[p]);
vector<P> res;
while(hs[u]!=hs[p]){
res.push_back({in[hs[u]],in[u]+1});
u=par[hs[u]];
}
res.push_back({in[p]+es,in[u]+1});
return res;
}
int jump(int u,int v,int k){
if(k<0)return -1;
int g=lca(u,v);
int d0=dist[u]+dist[v]-dist[g]*2;
if(d0<k)return -1;
int st=u;
if(dist[u]-dist[g]<k)st=v,k=d0-k;
for(;;){
int to=hs[st];
if(in[st]-k>=in[to])return rev[in[st]-k];
k-=in[st]-in[to]+1; st=par[to];
}
}
};
/**
* @brief Heavy Light Decomposition
*/
#line 5 "Verify/LC_lca.test.cpp"
int main(){
int n,q;
cin>>n>>q;
HLD hld(n);
rep(i,1,n){
int p;
cin>>p;
hld.add_edge(i,p);
}
hld.run();
while(q--){
int x,y;
cin>>x>>y;
cout<<hld.lca(x,y)<<'\n';
}
return 0;
}