#Codeforces Round #425 (Div. 2) D LCA + 树上路径交

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

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.

题目大意 :

有一棵树,在树上任意选取三个点, 现在让你找到一个最大值,即其中两个点向第三个点走的路径所经过的共同点数

思路 :

三个点怎样走是最优的可以不用考虑,直接全排列出所有的情况取一个最大值就行了,剩下的就是树上的路径交的问题,假设U和V是起点,T是终点,那么U到T和V到T这两条路径经过的公共点数就是(dis[U][T] + dis[V][T] - dis[U][V]) / 2,画画图就可以出来了,先找到路径,再消除重复部分,最后走了两次答案除以二,加上终点即可

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

vector <int> G[MAXN << 1];
int dis[MAXN], p[MAXN][25];
int dep[MAXN], rt[3], n, q;
void dfs(int x, int fa) {
	p[x][0] = fa;
	dep[x] = dep[fa] + 1;
	for (int i = 1; (1 << i) <= dep[x]; i++)
		p[x][i] = p[p[x][i - 1]][i - 1];
	for (auto it : G[x]) {
		if (it != fa) {
			dis[it] = dis[x] + 1;
			dfs(it, x);
		}
	}
}
int LCA(int xi, int yi) {
	if (dep[xi] > dep[yi]) swap(xi, yi);
	for (int i = 20; i >= 0; i--) {
		if (dep[yi] - (1 << i) >= dep[xi])
			yi = p[yi][i];
	}
	if (xi == yi) return xi;
	for (int i = 20; i >= 0; i--) {
		if (p[xi][i] == p[yi][i]) continue;
		xi = p[xi][i];
		yi = p[yi][i];
	}
	return p[xi][0];
}
int Query(int x, int y) {
	int lca = LCA(x, y);
	return dis[x] + dis[y] - 2 * dis[lca];
}

int main()
{
	cin >> n >> q;
	for (int i = 2; i <= n; i++) {
		int tmp; sc("%d", &tmp);
		G[tmp].push_back(i);
		G[i].push_back(tmp);
	}
	dfs(1, 0);
	while (q--) {
		for (int i = 0; i < 3; i++) sc("%d", &rt[i]);
		sort(rt, rt + 3);
		int ans = 0;
		do {
			int s1 = rt[0], s2 = rt[1], tp = rt[2];
			Max(ans, (Query(s1, tp) + Query(s2, tp) - Query(s1, s2)) / 2);
		} while (next_permutation(rt, rt + 3));
		printf("%d\n", ans + 1);
	}
	return 0;  // 改数组大小!!!
}
发布了213 篇原创文章 · 获赞 264 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43851525/article/details/104023517