Input
The first line contains two integers n and q (2 ≤ n ≤ 105, 1 ≤ 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 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.
Examples
input
3 2
1 1
1 2 3
2 3 3
output
2
3
input
4 1
1 2 3
1 2 3
output
The first line contains two integers n and q (2 ≤ n ≤ 105, 1 ≤ 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 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.
Examples
input
3 2
1 1
1 2 3
2 3 3
output
2
3
input
4 1
1 2 3
1 2 3
output
2
参考大神:http://blog.youkuaiyun.com/harlow_cheng/article/details/76039995
分析:给3个点任选一个作为终点其他两个为起始点,求两条路径相交的最大值。
求树上的路径可以用LCA。求两条路径的相交长度 为:(d(a,b) + d(c,b) - d(a,c)) /2 其中b为终点---这两条的路径必有相交点所以有此公式。
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#define siz 200005
const int maxn = 200000;
using namespace std;
int n,q;
int fa[siz];
vector<int>g[siz];
int cot,h[siz],first[siz],dp[siz*2][65],Id[siz*2];
void dfs(int u,int fa)
{
Id[++cot] = u, first[u] = cot, h[u] = h[fa]+1;
int len = g[u].size();
for(int i=0; i<len; i++)
{
int v = g[u][i];
if(v == fa) continue;
dfs(v,u);
Id[++cot] = u;
}
}
void Init()
{
cot = 0, h[1] = 0;
dfs(1,1);
for(int i=1; i<=cot; i++) dp[i][0] = Id[i];
for(int j=1; (1<<j)<=cot; j++)
{
for(int i=1; i+(1<<j)-1<=cot; i++)
{
int a = dp[i][j-1], b = dp[i+(1<<(j-1))][j-1];
if(h[a]<h[b]) dp[i][j] = a;
else dp[i][j] = b;
}
}
}
int LCA(int u,int v)
{
int s = first[u],e = first[v], j =0;
if(s>e) swap(s,e);
while((1<<(j+1))<=(e-s+1)) j++;
int a = dp[s][j], b = dp[e-(1<<j)+1][j];
if(h[a]<h[b]) return a;
else return b;
}
int dist(int a,int b)
{
int u = LCA(a,b);
int L = h[a]+h[b]-2*h[u];
return L;
}
int main()
{
int to,a,b,c;
while(~scanf("%d %d",&n,&q))
{
for(int i=2;i<=n;i++){
scanf("%d",&to);
g[i].push_back(to);
g[to].push_back(i);
}
Init();
for(int i=1;i<=q;i++){
scanf("%d %d %d",&a,&b,&c);
int ans = 0 ,lab,lac,lbc,l1,l2,l3;
lab = dist(a,b);
lac = dist(a,c);
lbc = dist(b,c);
l1 = (lab+lbc - lac)/2 + 1;
l2 = (lab+lac - lbc)/2 + 1;
l3 = (lac+lbc - lab)/2 + 1;
ans = max(l1,max(l2,l3));
printf("%d\n",ans);
}
}
return 0;
}