Cod sursa(job #953123)
| Utilizator | Data | 24 mai 2013 22:37:43 | |
|---|---|---|---|
| Problema | Lowest Common Ancestor | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.6 kb |
#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;
}
