Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground 最近公共祖先

本文介绍了一种结合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 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 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 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.



思路:LCA在线算法模板 + 数学。

求两点到同一点公共路径:(Sab + Sac - Sbc)/2  (  即a到b的距离 加上 a到c  的距离 减去 b到c 的距离 的一半 )重叠路径走了两次,题目问的是点,结果+1即可  

#include <bits/stdc++.h>
using namespace std;
const int AX = 1e5+666;
int n , q;
int tot,to;
int head[AX<<1];
int dist[AX];
int ver[AX<<1];
int vis[AX];
int R[AX<<1];
int first[AX<<1];
int dp[AX<<1][25];
int ans ;
struct Node{	
	int v,next;
}e[AX<<1];
inline int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	return x*f;
}
inline void addEdge(int u,int v){
	e[to].v = v;
	e[to].next = head[u];
	head[u] = to ++ ;
}
void init(){
	tot = 0;
	to = 0;
	ans = 0;
	memset(head,-1,sizeof(head));
	memset(vis,0,sizeof(vis));
}

void dfs(int u , int dep){
	vis[u] = 1; ver[++tot] = u; first[u] = tot;
	R[tot] = dep;
	for( int i = head[u] ; i!=-1 ;i = e[i].next ){
		if(!vis[e[i].v]){
			int v = e[i].v;
			dist[v] = dist[u] + 1;
			dfs(v,dep+1);
			ver[++tot] = u;
			R[tot] = dep;
		}
	}
}
void ST(){
	for( int i = 1 ; i <= tot ; i++ ){
		dp[i][0] = i;
	}
	for( int j = 1 ; (1 << j) <= tot; j ++ ){
		for( int i = 1 ; i + ( 1 << j ) - 1 <= tot ; i++ ){
			int a = dp[i][j-1];
			int b = dp[i + (1<<( j - 1 ))][j-1];
			dp[i][j] = R[a] < R[b] ? a : b;
		}
	}
}

int RMQ(int l , int r){
	int len = r - l + 1;
	int k = (int)(log((double)len) / log(2.0));
	int a = dp[l][k];
	int b = dp[ r - (1<<k) +1 ][k];
	return R[a] < R[b] ? a : b;
}

int LCA( int u ,int v ){
	int x = first[u];
	int y = first[v];
	if( x > y ) swap(x,y);
	int res = RMQ(x,y);
	return ver[res];
}

int main(){
	while( ~scanf("%d%d",&n,&q) ){
		init();
		int x;
		for( int i = 0 ; i < n-1 ; i++ ){
			x = read();
			addEdge(x,i+2);
			addEdge(i+2,x);
		}
		int a,b,c;	
		dfs(1,1);
		dist[1] = 0;
		ST();
		while( q-- ){
			ans = 0;
			a = read(); b = read(); c = read();
			int ab = LCA(a,b);
			int ac = LCA(a,c);
			int bc = LCA(b,c);
			int d_ab = dist[a] + dist[b] - 2*dist[ab];
			int d_ac = dist[a] + dist[c] - 2*dist[ac];
			int d_bc = dist[b] + dist[c] - 2*dist[bc];
			ans = max( d_ab + d_ac - d_bc , max( d_ab + d_bc - d_ac , d_bc + d_ac - d_ab ) );
			printf("%d\n",ans/2+1);
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值