codeforces832D. Misha, Grisha and Underground(倍增)

https://codeforces.com/problemset/problem/832/D

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 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 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 sftso 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

Copy

3 2
1 1
1 2 3
2 3 3

output

Copy

2
3

input

Copy

4 1
1 2 3
1 2 3

output

Copy

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.

第一次写倍增,记录一下,倍增的思想是:因为任何一个正整数能拆分成不同的2^i的和,所以能在预处理后,就行O(1)的区间查询。比线段树优点在于,还能求树上的区间,但好像不支持单点修改操作。求树上两点间的距离,再求最长的 两条边减去其他一条边的长度除以2。dp[i][node]记录node 2^ n的祖先节点。超过根节点时,一律表示为根节点0。

import java.util.*;
import java.io.*;

public class Main {
	public static void main(String args[]) {new Main().run();}

	FastReader in = new FastReader();
	PrintWriter out = new PrintWriter(System.out);
	void run(){
		work();
		out.flush();
	}
	long mod=998244353;
	long gcd(long a,long b) {
		return a==0?b:gcd(b%a,a);
	}
	int[][] dp;
	int[] dep;
	ArrayList<Integer>[] graph;
	void work() {//上右下左 0---7
		int n=ni(),q=ni();
		dp=new int[30][n];
		dep=new int[n];
		graph=ng(n,0);
		for(int i=1;i<n;i++) {
			int s=ni()-1;
			graph[s].add(i);
		}
		dfs(0,0,0);
		for(int i=0;i<q;i++) {
			int n1=ni()-1,n2=ni()-1,n3=ni()-1;
			int l1=find(n1,n2);
			int l2=find(n1,n3);
			int l3=find(n2,n3);
			int v1=(l1+l2-l3)/2;
			int v2=(l1+l3-l2)/2;
			int v3=(l2+l3-l1)/2;
			out.println(Math.max(v3,Math.max(v1, v2))+1);
		}
	}

	private int find(int n1, int n2) {
		int d=Math.min(dep[n1], dep[n2]);
		int cnt=0;
		//同一高度
		for(int i=29;i>=0;i--) {
			if(dep[n1]-(1<<i)>=d) {
				cnt+=(1<<i);
				n1=dp[i][n1];
			}
			if(dep[n2]-(1<<i)>=d) {
				cnt+=(1<<i);
				n2=dp[i][n2];
			}
		}
		//最后一个不一样的节点,或者前面已经相等
		for(int i=29;i>=0;i--) {
			if(dp[i][n1]!=dp[i][n2]) {
				cnt+=1<<(i+1);
				n1=dp[i][n1];
				n2=dp[i][n2];
			}
		}
		if(n1!=n2)cnt+=2;
		return cnt;
	}

	private void dfs(int node,int pre,int d) {
		int cur=pre;
		dp[0][node]=cur;
		dep[node]=d;
		for(int i=1;i<30;i++) {
			cur=dp[i-1][cur];
			dp[i][node]=cur;
		}
		for(int nn:graph[node]) {
			dfs(nn,node,d+1);
		}
	}



	//input
	private ArrayList<Integer>[] ng(int n, int m) {
		ArrayList<Integer>[] graph=(ArrayList<Integer>[])new ArrayList[n];
		for(int i=0;i<n;i++) {
			graph[i]=new ArrayList<>();
		}
		for(int i=1;i<=m;i++) {
			int s=in.nextInt()-1,e=in.nextInt()-1;
			graph[s].add(e);
			graph[e].add(s);
		}
		return graph;
	}

	private int ni() {
		return in.nextInt();
	}

	private long nl() {
		return in.nextLong();
	}

	private long[] na(int n) {
		long[] A=new long[n];
		for(int i=0;i<n;i++) {
			A[i]=in.nextLong();
		}
		return A;
	}
	private int[] nia(int n) {
		int[] A=new int[n];
		for(int i=0;i<n;i++) {
			A[i]=in.nextInt();
		}
		return A;
	}
}	

class FastReader
{
	BufferedReader br;
	StringTokenizer st;

	public FastReader()
	{
		br=new BufferedReader(new InputStreamReader(System.in));
	}


	public String next() 
	{
		while(st==null || !st.hasMoreElements())//回车,空行情况
		{
			try {
				st = new StringTokenizer(br.readLine());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return st.nextToken();
	}

	public int nextInt() 
	{
		return Integer.parseInt(next());
	}

	public long nextLong()
	{
		return Long.parseLong(next());
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值