Pagini recente » Cod sursa (job #3287289) | Rating Topala Alexandru (TopiAlex) | Cod sursa (job #144453) | Cod sursa (job #2199291) | Cod sursa (job #3164320)
//LCA cu smenul de la heavypath O(N + M log N)
#include <cstdio>
#include <vector>
using namespace std;
#define maxn 100010
int l[maxn], niv[maxn], d[maxn], tl[maxn];
int nl, n, m, x, y;
vector<int> v[maxn];
void df(int nod)
{
d[nod]=1;
if(v[nod].size()==0)
{
l[nod]=++nl;
return;
}
int fmax, it, dmax=0;
for(int i=0; i<v[nod].size(); ++i)
{
it=v[nod][i];
niv[it]=niv[nod]+1;
df(it);
d[nod]+=d[it];
if(d[it]>dmax)
{
dmax=d[it];
fmax=(it);
}
tl[l[it]]=nod;
}
l[nod]=l[fmax];
}
int solve(int x, int y)
{
while(l[x]!=l[y])
{
if(niv[tl[l[x]]]>niv[tl[l[y]]])
x=tl[l[x]];
else
y=tl[l[y]];
}
if(niv[x]<niv[y])
return x;
return y;
}
int main()
{
freopen("lca.in", "r", stdin);
freopen("lca.out", "w", stdout);
scanf("%d%d", &n, &m);
for(int i=2; i<=n; ++i)
{
scanf("%d", &x);
v[x].push_back(i);
}
niv[1]=1;
df(1);
tl[l[1]]=0;
while(m--)
{
scanf("%d%d", &x, &y);
printf("%d\n", solve(x, y));
}
return 0;
}