Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground 最近公共祖先

来源:互联网 发布:占卜软件哪个好 编辑:程序博客网 时间:2024/05/22 00:24
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 integerpi 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.



思路:LCA在线算法模板 + 数学。

求两点到同一点公共路径:(Sab + Sac - Sbc)/2  (  即a到b的距离 加上 a到c  的距离 减去 b到c 的距离 的一半 )重叠路径走了两次,题目问的是点,结果+1即可  

#include <bits/stdc++.h>using namespace std;const int AX = 1e5+666;int n , q;int tot,to;int head[AX<<1];int dist[AX];int ver[AX<<1];int vis[AX];int R[AX<<1];int first[AX<<1];int dp[AX<<1][25];int ans ;struct Node{int v,next;}e[AX<<1];inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;}inline void addEdge(int u,int v){e[to].v = v;e[to].next = head[u];head[u] = to ++ ;}void init(){tot = 0;to = 0;ans = 0;memset(head,-1,sizeof(head));memset(vis,0,sizeof(vis));}void dfs(int u , int dep){vis[u] = 1; ver[++tot] = u; first[u] = tot;R[tot] = dep;for( int i = head[u] ; i!=-1 ;i = e[i].next ){if(!vis[e[i].v]){int v = e[i].v;dist[v] = dist[u] + 1;dfs(v,dep+1);ver[++tot] = u;R[tot] = dep;}}}void ST(){for( int i = 1 ; i <= tot ; i++ ){dp[i][0] = i;}for( int j = 1 ; (1 << j) <= tot; j ++ ){for( int i = 1 ; i + ( 1 << j ) - 1 <= tot ; i++ ){int a = dp[i][j-1];int b = dp[i + (1<<( j - 1 ))][j-1];dp[i][j] = R[a] < R[b] ? a : b;}}}int RMQ(int l , int r){int len = r - l + 1;int k = (int)(log((double)len) / log(2.0));int a = dp[l][k];int b = dp[ r - (1<<k) +1 ][k];return R[a] < R[b] ? a : b;}int LCA( int u ,int v ){int x = first[u];int y = first[v];if( x > y ) swap(x,y);int res = RMQ(x,y);return ver[res];}int main(){while( ~scanf("%d%d",&n,&q) ){init();int x;for( int i = 0 ; i < n-1 ; i++ ){x = read();addEdge(x,i+2);addEdge(i+2,x);}int a,b,c;dfs(1,1);dist[1] = 0;ST();while( q-- ){ans = 0;a = read(); b = read(); c = read();int ab = LCA(a,b);int ac = LCA(a,c);int bc = LCA(b,c);int d_ab = dist[a] + dist[b] - 2*dist[ab];int d_ac = dist[a] + dist[c] - 2*dist[ac];int d_bc = dist[b] + dist[c] - 2*dist[bc];ans = max( d_ab + d_ac - d_bc , max( d_ab + d_bc - d_ac , d_bc + d_ac - d_ab ) );printf("%d\n",ans/2+1);}}return 0;}


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