[codeforces 832D]Misha, Grisha and Underground

来源:互联网 发布:无印良品有淘宝店吗 编辑:程序博客网 时间:2024/06/03 20:52

time limit per test : 2 seconds
memory limit per test : 256 megabytes

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 a, b 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 s, 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 and q (2 ≤ n ≤ 10^5, 1 ≤ q ≤ 10^5) — 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 a, b 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 s, t and f are chosen optimally from the three stations on the i-th day.

样例输入1:

3 21 11 2 32 3 3

样例输出1:

23

样例输入2:

4 11 2 31 2 3

样例输出2:

2

Note

In the first example on the first day if s = 1, f = 2, t = 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 = 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 route 1->2->3, and Grisha would go on the route 2->3 and would see the text at both stations.

题解:沿袭CF的传统,题面又臭又长。
解释一下题意
给定一棵树,每次询问三个数字a,b,c。你可以任取两个当做起点,最后一个当做终点,求问两个人分别从不同的起点出发,经过的相同节点数是多少。

题解:LCA+路程判定。用ab表示a到b的距离,ac表示a到c的距离,bc表示b到c的距离。那么假设a是终点,那么两个人共同走的距离就是(ab+ac-bc)/2。那么经过的相同节点数自然就是共同距离+1啦。

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#include<queue>#include<vector>#define LiangJiaJun main#define pa pair<int,int>#define INF 1999122700ll#define ll long longusing namespace std;struct edge{    int to,nt;}e[200004];int ne=1,h[100004];void insert(int u,int v){     e[++ne].to =v;e[ne].nt =h[u];h[u]=ne;}int fa[100004][24],deepth[100004];int n,q,a[4],b[4];void dfs(int F,int now){     if(deepth[now])return ;     deepth[now]=deepth[F]+1;     fa[now][0]=F;     for(int i=1;fa[fa[now][i-1]][i-1];i++)fa[now][i]=fa[fa[now][i-1]][i-1];     for(int i=h[now];i;i=e[i].nt)dfs(now,e[i].to);}int LCA(int a,int b){    if(deepth[a]<deepth[b])swap(a,b);    int dva=deepth[a]-deepth[b];    if(deepth[a]>deepth[b]){       for(int i=0;i<=17;i++){           if(dva&(1<<i))a=fa[a][i];       }    }    if(a!=b){       for(int i=17;i>=0;i--){           if(fa[a][i]!=fa[b][i]){               a=fa[a][i];               b=fa[b][i];           }       }    }    if(a==b)return a;    else return fa[a][0];}int work(int a,int b){    int t=LCA(a,b);    return deepth[a]+deepth[b]-deepth[t]*2;}int LiangJiaJun(){    scanf("%d%d",&n,&q);    for(int i=2;i<=n;i++){        int u;        scanf("%d",&u);        insert(i,u);insert(u,i);    }    deepth[1]=1;    for(int i=h[1];i;i=e[i].nt)dfs(1,e[i].to);    for(int i=1;i<=q;i++){        scanf("%d%d%d",&a[1],&a[2],&a[3]);        int cnt=0,ud=0;        int ab,ac,bc;        ab=work(a[1],a[2]);        ac=work(a[1],a[3]);        bc=work(a[2],a[3]);        printf("%d\n",(max(ab+ac-bc,max(ab+bc-ac,bc+ac-ab)))/2+1);    }    return 0;}
原创粉丝点击