【Codeforces】832D. Misha, Grisha and Underground

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 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 ≤ 1051 ≤ 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 = 1f = 2t = 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 = 3f = 2t = 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 = 1f = 3t = 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.



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



题目大意: 给你一棵树, n个点,有q个询问, 每一次询问给出三个点a, b, c 问从其中两点到第三点的最长公共路径是多长。 (n ,q <= 1e5)


解题思路: 这是一道LCA的题目。

对于三个点来说, LCA的一些性质: 三个点任意两点求出的三个lca 必定会有两个相同的点。那么第三个lca就是他们的公共点。 最长公共路径就是从公共点到三个点的最长的路径。

并且今天才发现 对于三个lca直接^一下就可以了。



之前做过一道题, 让三个人走到一个点的最小花费,  就是转化成三个点到公共点距离的和。 

别人的博客里看到这样的一个公式:

求树上的路径可以用LCA。求两条路径的相交长度 为:(d(a,b) + d(c,b) - d(a,c)) /2  其中b为终点---这两条的路径必有相交点所以有此公式。

//2017-7-28 15:28
//2017-7-28 16:30
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<algorithm>
using namespace std;
const int MaxN = 1e5;
int fa[MaxN + 5], dis[MaxN + 5];
int up[MaxN + 5][30];
vector<int>edge[MaxN + 5];

int n, q;

void dfs(int u, int dep){
	dis[u] = dep;
	for(int i = 0; i < edge[u].size(); i++){
		int v = edge[u][i];
		if(!dis[v]){
			up[v][0] = u;
			dfs(v, dep + 1);
		}
	}
}

void init_lca(){
	for(int j = 1; (1 << j) <= n; j++)
		for(int i = 1; i <= n; i++)
			up[i][j] = up[up[i][j - 1]][j - 1];
}

int LCA(int a, int b){
	if(dis[a] < dis[b]) swap(a, b);
	int c= dis[a] - dis[b];
	for(int i = 0; (1 << i) <= n; i++)
		if(c & (1 << i)) a = up[a][i];
	if(a == b) return a;
	for(int i = 25; i >= 0; i--){
		if(up[a][i] != up[b][i]){
			a = up[a][i];
			b = up[b][i];
		}
	}
	return up[a][0];
}

int len(int a, int b){
	int lca = LCA(a,b);
	return dis[a] + dis[b] - 2 * dis[lca] + 1;
}

int main(){
	while(~scanf("%d %d", &n, &q)){
		for(int i = 2; i <= n; i++){
			int x;
			scanf("%d", &x);
			edge[i].push_back(x);
			edge[x].push_back(i);
		}
		dfs(1, 1);
		init_lca();
		while(q--){
			int a, b, c;
			scanf("%d %d %d", &a, &b, &c);
			int lca1 = LCA(a, b);
			int lca2 = LCA(b, c);
			int lca3 = LCA(a, c);
			int l = lca1 ^ lca2 ^ lca3;
			int ans = max(len(a, l), max(len(b, l), len(c, l)));
			printf("%d\n", ans);
		}
	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值