Codeforces 832D Misha, Grisha and Underground【LCA】

来源:互联网 发布:淘宝客服自我评价 编辑:程序博客网 时间:2024/06/06 05:44

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 hasn 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 stations 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 (includings and f). After that on the same day at evening Grisha will ride from stationt 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 a,b and c for each of several following days, one of them should be stations on that day, another should be station f, and the remaining should be station t. They became interested how they should choose these stationss, f,t so that the number Grisha will count is as large as possible. They asked you for help.

Input

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

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

The next q lines contains three integersa, b andc 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 stationss, t andf are chosen optimally from the three stations on thei-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 =1, f = 2, t = 3, Misha would go on the route1 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 = 3, f =2, t = 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 = 1, f = 3,t = 2, Misha would go on the route1 2 3, and Grisha would go on the route 2 3 and would see the text at both stations.


题目大意:


给你一棵树,每组查询三个点a,b,c,让你从中选取一个点作为终点,另外两个点作为起点,我们要求两个起点到终点的路径上,重复经过的点的个数最多,问这个最多点数。


思路:


设定S1,S2表示两个起点,T表示终点的话,如果我们能够知道S1->T S2->T的两条路径上重复覆盖的长度的话(LEN),我们就能够知道结果的点数(LEN+1)。


那么考虑求两条路径上的重复长度。


我们知道,求树上两点间距离需要求两点LCA,那么我们预处理倍增然后在线查询就行。


那么我们可以求出从S1到T的路径长度,也能求出S2到T的路径长度,那么求和后,再减去S1到LCA(S1,S2)的路径长度,再减去S2到LCA(S1,S2)的路径长度之后,得到的长度就是覆盖的重复路径长度*2,最终结果也就是这个结果/2+1。


Ac代码:

#include<stdio.h>#include<string.h>#include<vector>#include<math.h>using namespace std;#define N 100050struct node{    int from;    int to;    int w;    int next;}e[350000];int cont,n,m;int p[150000][20];int d[150000];int dist[150000];int head[150000];void add(int from,int to,int w){    e[cont].to=to;    e[cont].w=w;    e[cont].next=head[from];    head[from]=cont++;}void Dfs(int u,int from){    for(int i=head[u];i!=-1;i=e[i].next)    {        int v=e[i].to;        if(v==from)continue;        d[v]=d[u]+1;        dist[v]=dist[u]+1;        p[v][0]=u;        Dfs(v,u);    }}void init(){    for(int j=1;(1<<j)<=n;j++)    {        for(int i=1;i<=n;i++)        {            p[i][j]=p[p[i][j-1]][j-1];        }    }}int Lca(int x,int y){    if(d[x]>d[y])swap(x,y);    int f=d[y]-d[x];    for(int i=0;(1<<i)<=f;i++)    {        if((1<<i)&f)y=p[y][i];    }    if(x!=y)    {        for(int i=(int)log2(N);i>=0;i--)        {            if(p[x][i]!=p[y][i])            {                x=p[x][i];                y=p[y][i];            }        }        x=p[x][0];    }    return x;}int Dist(int a,int b){    return dist[a]+dist[b]-2*dist[Lca(a,b)];}int main(){    while(~scanf("%d%d",&n,&m))    {        cont=0;        memset(d,0,sizeof(d));        memset(dist,0,sizeof(dist));        memset(head,-1,sizeof(head));        for(int i=2;i<=n;i++)        {            int x;scanf("%d",&x);            add(x,i,1);            add(i,x,1);        }        Dfs(1,-1);        init();        while(m--)        {            int a,b,c;            scanf("%d%d%d",&a,&b,&c);            int ans=0;            ans=max(Dist(b,a)+Dist(c,a)-Dist(b,Lca(b,c))-Dist(c,Lca(b,c)),ans);            ans=max(Dist(a,b)+Dist(c,b)-Dist(a,Lca(a,c))-Dist(c,Lca(a,c)),ans);            ans=max(Dist(a,c)+Dist(b,c)-Dist(a,Lca(a,b))-Dist(b,Lca(a,b)),ans);            printf("%d\n",ans/2+1);        }    }}







阅读全文
0 0
原创粉丝点击