CF-832-D- Misha, Grisha and Underground(lca倍增,板子+规律)

题目链接:http://codeforces.com/problemset/problem/832/D

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 sand 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 ab 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 sft 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 ≤ 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 ab 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 st 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

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.

题目大意:给出n个车站,m次访问

n-1个数字,意思为i-pi的道路是通路(双向)

之后是m次访问,输入三个点,任意一个是终点,另两个是起点,找出重合点最多的路线,输出重合点的数量

其实并不难,关键是如何确定重合最多的点,因为就三个点,可以直接处理处三个情况,然后取最大的即可;

对于a,b,c三点;很容易从图中看到,如果以a,b为起点,那么(lca(a,c)+lca(b,c)-lca(a,b))/2就是他们的重合的边了,那么因为有三条边,直接找出三条边的长度然后取最长的那个即可:

ac:

//#pragma comment(linker, "/STACK:1024000000,1024000000") 

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<fstream>
#include<iostream>  
#include<algorithm>  
using namespace std;  

#define ll long long  
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
//std::ios::sync_with_stdio(false);
const int MAXN=101000;
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;

struct node{
	int v,w,nxt;
	node(int _v=0,int _nxt=0):
	v(_v),nxt(_nxt){}
}edge[MAXN<<1];
int head[MAXN],ecnt;
int fa[MAXN][30],deep[MAXN];
int len[MAXN][30];
int n,m;
void intt()
{
	clean(len,0);
	clean(head,-1);
	clean(deep,0);
	clean(fa,-1);
	ecnt=0;
}
 
void add(int u,int v)
{
	edge[ecnt]=node(v,head[u]);
	head[u]=ecnt++;
}
/*---------------板子-----------------*/
void dfs(int u)
{
	for(int i=head[u];i+1;i=edge[i].nxt)
	{
		int temp=edge[i].v;
		if(deep[temp]==0)
		{
			deep[temp]=deep[u]+1;
			fa[temp][0]=u;
			len[temp][0]=1;
			int up=0,pre=u;
			while(fa[pre][up]>=0)
			{
				fa[temp][up+1]=fa[pre][up];
				len[temp][up+1]=len[temp][up]+len[pre][up];
				pre=fa[pre][up++];
			}
			dfs(temp);
		}
	}
}

int lca(int a,int b)
{
	int ans=0;
	if(deep[a]<deep[b])
		swap(a,b);
	int lim=log2(deep[a])+1;
	for(int i=lim;i>=0;--i)
	{
		if(deep[fa[a][i]]>=deep[b])
		{
			ans+=len[a][i];
			a=fa[a][i];
		}
	}
	if(a==b)
		return ans;
	for(int i=lim;i>=0;--i)
	{
		if(fa[a][i]!=fa[b][i])
		{
			ans=ans+len[a][i];
			a=fa[a][i];
			ans=ans+len[b][i];
			b=fa[b][i];
		}
	}
	ans=ans+len[a][0]+len[b][0];
	return ans;
}

int main()
{
	std::ios::sync_with_stdio(false);
	intt();
	cin>>n>>m;
	int num;
	for(int i=2;i<=n;++i)
	{
		cin>>num;
		add(i,num);
		add(num,i);
	}
	deep[1]=1;
	dfs(1);
	int a,b,c;
	for(int i=1;i<=m;++i)
	{
		cin>>a>>b>>c;
		//以任意点为终点,另两个点为起点,重合的最多的点 
		int lab=lca(a,b);
		int lac=lca(a,c);
		int lbc=lca(b,c);
		int ans1=(lac+lbc-lab)/2+1;
		int ans2=(lac+lab-lbc)/2+1;
		int ans3=(lab+lbc-lac)/2+1;
		cout<<max(ans1,max(ans2,ans3))<<endl;
		
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/82776097