欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

codeforce 823 D (倍增LCA)

程序员文章站 2022-04-13 11:44:23
...

Descirption

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 ≤ 105, 1 ≤ q ≤ 105) — the number of stations and the number of days.

The second line contains n - 1 integers p 2, p 3, …, p n (1 ≤ p i ≤ n). The integer p i means that there is a route between stations p i 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

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.

题意

给定一个1e5数量级的最小生成树,并查询1e5次,每次查询指定三个点,求出来两两间最短路径的最长相交部分的点个数
假设我们把题中给的图变成了一棵有根树,并且有了一组样例A,B,C,我们标注出AB,AC,BC间的距离,得到下图:
codeforce 823 D (倍增LCA)
通过研究该图我们可以发现最长重复距离S=(AB+AC-BC)/2.即S=(最长+次长-最短)/2
现在问题在于如何快速的求出AB,AC,BC,即快速求出任意两点间距离.极端情况下:用DFS,时间复杂度为O(n),每次要遍历1e5个点,而测试样例最多可有1e5组,1e5*1e5显然超时了.我们需要O(logn)或O(1)的算法.
这里我们用上了倍增LCA,参考资料非常多,可以看这篇:树上倍增求LCA详解
等你学会了倍增LCA再来看这个题: AB的距离=depth(A)+dep(B)-dep(lca(A,B))
如此以来就能求出S=(AB+AC-BC)/2
此时先别急着交,题目要求的是点的个数,而我们求的是边的长度.想一下可知,一条路径上点数=边数+1
故最终ans=(最长+次长-最短)/2+1

AC代码

#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e5+5;
int n,m,s,num=0,head[MAXN*2],dep[MAXN*2],f[MAXN*2][23];
struct edg{
    int next,to;
}edge[MAXN*2];
void edge_add(int u,int v)//链式前向星存图 
{
    num++;
    edge[num].next=head[u];edge[num].to=v;head[u]=num;
    edge[++num].next=head[v];edge[num].to=u;head[v]=num;
}
void dfs(int u,int father)//对应深搜预处理f数组 
{
    dep[u]=dep[father]+1;
    for(int i=1;(1<<i)<=dep[u];i++)
    {
        f[u][i]=f[f[u][i-1]][i-1];
    }
    for(int i=head[u];i;i=edge[i].next)
    {
        int v=edge[i].to;
        if(v==father)continue;//双向图需要判断是不是父亲节点 
        f[v][0]=u;
        dfs(v,u);
    }
}
int lca(int x,int y)
{
    if(dep[x]<dep[y])swap(x,y);
    for(int i=20;i>=0;i--)//从大到小枚举使x和y到了同一层 
    {
        if(dep[f[x][i]]>=dep[y])x=f[x][i];
        if(x==y)return x;
    }
    for(int i=20;i>=0;i--)//从大到小枚举 
    {
        if(f[x][i]!=f[y][i])//尽可能接近 
        {
            x=f[x][i];y=f[y][i];
        } 
    } 
    return f[x][0];//随便找一个**输出 
}
int main(){
	ios::sync_with_stdio(0);
	int n,m;
	cin>>n>>m;
	for(int i=2;i<=n;i++){
		int u;
		cin>>u;
		edge_add(u,i);
	}
	dep[0]=-1;
	dfs(1,0);
	int dis[3];
	int a,b,c;
	for(int i=0;i<m;i++){
		cin>>a>>b>>c;
		dis[0]=dep[a]+dep[b]-2*dep[lca(a,b)];
		dis[1]=dep[a]+dep[c]-2*dep[lca(a,c)];
		dis[2]=dep[b]+dep[c]-2*dep[lca(b,c)];
		sort(dis,dis+3);
		cout<<(dis[2]+dis[1]-dis[0])/2+1<<"\n";
	}
	return 0;
}