Professional Documents
Culture Documents
LCA
LCA
h
#define ios iossync_with_stdio(0); cin.tie(NULL); cout.tie(NULL)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define enter cout 'n'
ifstream fin(lca.in);
ofstream fout(lca.out);
vi graph[NMAX];
vii line;
pii rmq[EMAX][LMAX];
int n, m;
int ap[NMAX], lg[LMAX];
bool vis[NMAX];
void build()
{
lg[1] = 0;
for (int i = 2; i = line.size(); ++i)
lg[i] = lg[i 2] + 1;
void read()
{
int x;
cin n m;
for (int i = 2; i = n; ++i)
{
cin x;
graph[i].pb(x);
graph[x].pb(i);
}
}
int main()
{
read();
dfs(1, 0);
build();
while (m--)
{
int st, dr;
cin st dr;
st = ap[st];
dr = ap[dr];
if (st dr) swap(st, dr);
cout query(st, dr).se 'n';
}
return 0;
}