lca

来源:互联网 发布:淘宝运费险如何使用 编辑:程序博客网 时间:2024/06/01 22:29
D. Misha, Grisha and Underground
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations connected with n - 1 routes so that each route connects two stations, and it is possible to reach every station from any other.

The boys decided to have fun and came up with a plan. Namely, in some day in the morning Misha will ride the underground from station s to station f by the shortest path, and will draw with aerosol an ugly text "Misha was here" on every station he will pass through (including s and f). After that on the same day at evening Grisha will ride from station t to station f by the shortest path and will count stations with Misha's text. After that at night the underground workers will wash the texts out, because the underground should be clean.

The boys have already chosen three stations ab and c for each of several following days, one of them should be station s on that day, another should be station f, and the remaining should be station t. They became interested how they should choose these stations sftso that the number Grisha will count is as large as possible. They asked you for help.

Input

The first line contains two integers n and q (2 ≤ n ≤ 1051 ≤ q ≤ 105) — the number of stations and the number of days.

The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi ≤ n). The integer pi means that there is a route between stations pi and i. It is guaranteed that it's possible to reach every station from any other.

The next q lines contains three integers ab and c each (1 ≤ a, b, c ≤ n) — the ids of stations chosen by boys for some day. Note that some of these ids could be same.

Output

Print q lines. In the i-th of these lines print the maximum possible number Grisha can get counting when the stations st and f are chosen optimally from the three stations on the i-th day.

Examples
input
3 21 11 2 32 3 3
output
23
input
4 11 2 31 2 3
output
2
Note

In the first example on the first day if s = 1f = 2t = 3, Misha would go on the route 1  2, and Grisha would go on the route 3  1  2. He would see the text at the stations 1 and 2. On the second day, if s = 3f = 2t = 3, both boys would go on the route 3  1  2. Grisha would see the text at 3 stations.

In the second examle if s = 1f = 3t = 2, Misha would go on the route 1  2  3, and Grisha would go on the route 2  3 and would see the text at both stations.

给你一颗树,给你三个点选两个点作为起点,一个点作为终点,问这两条路径的最长重合的部分是多少,sort(b,b+3);答案=(b[2]+b[1]-b[0])/2+1;大概就是这个样子吧。
#include<bits/stdc++.h>using namespace std;const int maxn=1000005;int fir[maxn],edg[maxn],nex[maxn],fa[maxn][40],ecnt;int depth[maxn];inline void add(int f,int s){    edg[ecnt]=s,nex[ecnt]=fir[f],fir[f]=ecnt++,fa[s][0]=f;}void dfs(int rt,int f){    for(int i=1; i<40; i++)    {        if(depth[rt]<(1<<i))            break;        fa[rt][i]=fa[fa[rt][i-1]][i-1];    }    for(int i=fir[rt]; i!=-1; i=nex[i])    {        if(edg[i]==f)            continue;        depth[edg[i]]=depth[rt]+1;        fa[edg[i]][0]=rt;        dfs(edg[i],rt);    }}int lca(int a,int b){    if(depth[a]<depth[b])    {        std::swap(a,b);    }    int t=depth[a]-depth[b];    for(int i=0; i<25; i++)        if((1<<i)&t)            a=fa[a][i];    for(int i=39; i>=0; i--)    {        if(fa[a][i]!=fa[b][i])            a=fa[a][i],b=fa[b][i];    }    if(a!=b)        return fa[a][0];    return a;}int juli(int a,int b){    int c=lca(a,b);    return depth[a]-depth[c]+depth[b]-depth[c]+1;}int main(){    int n,m;    memset(fir,-1,sizeof(fir));    memset(depth,0,sizeof(depth));    memset(fa,0,sizeof(fa));    ecnt=0;    scanf("%d%d",&n,&m);    for(int i=2; i<=n; i++)    {        int q;        scanf("%d",&q);        add(i,q);        add(q,i);    }    dfs(1,-1);    while(m--)    {        int a[10];        scanf("%d%d%d",&a[0],&a[1],&a[2]);        int b[10];        b[0]=juli(a[0],a[1]);        b[1]=juli(a[1],a[2]);        b[2]=juli(a[2],a[0]);        sort(b,b+3);        printf("%d\n",(b[1]+b[2]-b[0])/2+1);    }}