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.
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.
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.
3 2
1 1
1 2 3
2 3 3
2
3
4 1
1 2 3
1 2 3
2
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.
题意:给出n个结点的一棵树,有q次询问,对于每次询问,给出三个点,要求从三个点中选出两个起点一个终点,使得两条起点到终点路径上共同经过的结点数最多
题解:倍增LCA,求出三个点两两的公共祖先,层次最深的就是三个点的三叉路口,求出三个点到三叉路口距离,取距离最大的,再加一即为答案。
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include<stdio.h>
#include <vector>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define MAXN 100005
using namespace std;
const long long DEG = 22;
typedef long long LL;
struct SEdge
{
LL end;
LL value;
LL next;
}edge[MAXN*2];
LL head[MAXN], CntEdge;
LL Dist[MAXN];
LL Pre[MAXN][DEG];
LL Ans[MAXN];
bool Visited[MAXN];
LL pre[MAXN];
int v[MAXN];
void Initial()
{
memset( head, -1, sizeof(head) );
memset(pre,0,sizeof(pre));
memset(Ans,0,sizeof(Ans));
memset(v,0,sizeof(v));
memset(Dist,0,sizeof(Dist));
memset(edge,0,sizeof(edge));
memset(Visited,0,sizeof(Visited));
memset(Pre,0,sizeof(Pre));
CntEdge = 0;
}
void AddEdge( LL start, LL end, LL value )
{
edge[CntEdge].end = end;
edge[CntEdge].value = value;
edge[CntEdge].next = head[start];
head[start] = CntEdge++;
}
void DFS( LL Now )
{
for( LL j = 1; j < DEG; j++ )
Pre[Now][j] = Pre[Pre[Now][j-1]][j-1];
Visited[Now] = true;
for( LL i = head[Now]; i != -1; i = edge[i].next )
{
LL temp = edge[i].end;
if( Visited[temp] )
continue;
pre[temp]=Now;
Dist[temp] = Dist[Now] + edge[i].value;
Pre[temp][0] = Now;
DFS( temp );
}
}
LL LCA( LL node1, LL node2 )
{
LL i;
if( Dist[node1] < Dist[node2] )
swap( node1, node2 );
for( LL i = 0, d=Dist[node1]-Dist[node2]; d; i++, d>>=1 )
{
if( d&1 )
node1 = Pre[node1][i];
}
if( node1 == node2 )
return node1;
for( LL i = DEG-1; i >= 0; i-- )
if( Pre[node1][i] != Pre[node2][i] )
node1 = Pre[node1][i], node2 = Pre[node2][i];
return Pre[node1][0];
}
LL Distance( LL node1, LL node2 )
{
return Dist[node1] + Dist[node2] - 2*Dist[LCA(node1, node2)];
}
LL sum;
int main()
{
LL n, q;
while(scanf("%lld%lld",&n,&q) != EOF)
{
Initial();
for( LL i = 2; i <= n; i++ )
{
LL ss;
scanf("%lld",&ss);
AddEdge( i, ss, 1 );
AddEdge( ss,i, 1 );
}
DFS(1);
for( LL i = 0; i < q; i++ )
{
LL a,b,c;
scanf("%lld%lld%lld",&a,&b,&c);//cout<<"sf";
LL an1=LCA(a,b);
LL an2=LCA(a,c);
LL an3=LCA(b,c);
LL tt;
if(Dist[an1]>Dist[an2])
tt=an1;
else tt=an2;
if(Dist[an3]>Dist[tt])
tt=an3;
LL an=max(Distance(a,tt),Distance(b,tt));
Ans[i]=max(an,Distance(c,tt))+1;
}
for( LL i = 0; i < q; i++ )
printf("%lld\n", Ans[i]);
}
return 0;
}