Pagini recente » Cod sursa (job #2800683) | Cod sursa (job #1716373) | Cod sursa (job #484367) | Cod sursa (job #2184184) | Cod sursa (job #572413)
Cod sursa(job #572413)
#include<cstdio>
#include<vector>
#include<cmath>
#define Vmax 300001
#define Nmax 100001
#define logV 20
using namespace std;
int N,M,m[logV][Vmax],poz[Vmax];
vector <pair <int,int> > v;
vector <int> a[Nmax];
void dfs(int nod,int depth){
v.push_back(make_pair(nod,depth));
poz[nod]=v.size();
for(vector<int>::iterator it = a[nod].begin(); it!=a[nod].end(); ++it){
dfs(*it,depth+1);
v.push_back(make_pair(nod,depth));
}
}
void rmq(){
int Z=v.size();
for(int i=0;i<Z;++i)
m[0][i+1]=i;
for(int lung=1; (1<<lung) <=Z; ++lung)
for(int start=1; start+ ( 1 << (lung-1) ) <=Z ; ++start)
if(v[m[lung-1][start]].second <= v[m[lung-1][start + (1<< (lung-1))]].second )
m[lung][start] = m[lung-1][start];
else m[lung][start] = m[lung-1][start + (1<< (lung-1))];
}
int main(){
freopen("lca.in","r",stdin);
freopen("lca.out","w",stdout);
scanf("%d%d",&N,&M);
for(int i=1;i<N;++i){
int x;
scanf("%d",&x);
a[x].push_back(i+1);
}
dfs(1,1);
rmq();
for(int i=1;i<=M;++i){
int x,y;
scanf("%d%d",&x,&y);
x=poz[x];
y=poz[y];
if(x>y) swap(x,y);
int lgl =(int)log2(y-x+1);
if( v[m[lgl][x]].second < v[m[lgl][y - (1<<lgl)+1]].second )
printf("%d\n",m[lgl][x]);
else printf("%d\n",v[m[lgl][y - (1<<lgl)+1]].first);
}
return 0;
}