Cod sursa(job #2495414)
| Utilizator | Data | 19 noiembrie 2019 12:45:04 | |
|---|---|---|---|
| Problema | Lowest Common Ancestor | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.58 kb |
return sqlca(u,sqparent[v]);
}
return naivelca(u,v);
}
int main()
{
freopen("lca.in","r",stdin);
freopen("lca.out","w",stdout);
int m,n,i,j;
scanf("%d%d",&m,&n);
for(i=2;i<=m;i++)
{scanf("%d",&parent[i]);v[parent[i]].push_back(i);}
dfs(1);
parent[1]=0;
depth[0]=-1;
h=(int)sqrt((double)depth_max);
fill(depth,depth+NMAX,0);
dfs1(1);
for(i=1;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
int ans=sqlca(x,y);
printf("%d\n",ans);
}
return 0;
}
