Pagini recente » Cod sursa (job #3153333) | Cod sursa (job #2377135) | Cod sursa (job #1338002) | Cod sursa (job #1662565) | Cod sursa (job #953123)
Cod sursa(job #953123)
#include<cstdio>
int n,m,i,j,q,t,x,y,tata[100007],viz[100007];
bool ok;
int main()
{
freopen("lca.in","r",stdin);
freopen("lca.out","w",stdout);
scanf("%d %d",&n,&m);
for(i=1;i<n;i++)scanf("%d",&tata[i+1]);
for(i=1;i<=m;i++){
scanf("%d %d",&x,&y);
ok=true;
viz[x]=i;
viz[y]=i;
while(ok==true){
x=tata[x];
if(viz[x]==i)ok=false;
viz[x]=i;
y=tata[y];
if(viz[y]==i && ok==true)ok=false,x=y;
viz[y]=i;
}
printf("%d\n",x);
}
return 0;
}