Codeforces Round #425 (Div. 2) Misha, Grisha and Underground(LCA)

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 sto 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 ≤ 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
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  2, and Grisha would go on the route   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   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   3, and Grisha would go on the route  3 and would see the text at both stations.

 【题意】给你一棵树,然后q次询问,每次给出三个点a,b,c,要求从其中两个点走到第三个点,使得两种路径中共同经过的点最多,最多是多少?

 【分析】枚举终点,然后判一下就行了。

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 2e5+50;
const int M = 16000009;
const int mod = 1e9+7;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n,m,ans;
int dep[N],fa[N][20];
vector<int>edg[N];
void dfs(int u,int f){
    fa[u][0]=f;
    for(int i=1;i<20;i++){
        fa[u][i]=fa[fa[u][i-1]][i-1];
    }
    for(int v : edg[u]){
        if(v==f)continue;
        dep[v]=dep[u]+1;
        dfs(v,u);
    }
}
int LCA(int u,int v){
    int U=u,V=v;
    if(dep[u]<dep[v])swap(u,v);
    for(int i=19;i>=0;i--){
        if(dep[fa[u][i]]>=dep[v]){
            u=fa[u][i];
        }
    }
    if(u==v)return (u);
    for(int i=19;i>=0;i--){
        if(fa[u][i]!=fa[v][i]){
            u=fa[u][i];v=fa[v][i];
        }
    }
    return (fa[u][0]);
}
void solve(int u,int v,int t){
    int ut=LCA(u,t);
    int vt=LCA(v,t);
    if(dep[ut]<dep[t]&&dep[vt]<dep[t]){
        if(ut==vt){
            int uv=LCA(u,v);
            ans=max(ans,dep[t]-max(dep[ut],dep[vt])+1+dep[uv]-dep[ut]);
        }
        else ans=max(ans,dep[t]-max(dep[ut],dep[vt])+1);
    }
    else if(ut==t&&vt==t){
        int uv=LCA(u,v);
        if(uv!=t)ans=max(ans,dep[uv]-dep[t]+1);
        else ans=max(ans,1);
    }
    else ans=max(ans,1);
}
int main(){
    scanf("%d%d",&n,&m);
    for (int i=2,v;i<=n;i++){
        scanf("%d",&v);
        edg[i].pb(v);
        edg[v].pb(i);
    }
    dep[1]=1;
    dfs(1,0);
    while(m--){
        int a,b,c;
        ans=0;
        scanf("%d%d%d",&a,&b,&c);
        solve(a,b,c);
        solve(a,c,b);
        solve(b,c,a);
        printf("%d\n",ans);
    }
    return 0;
}

 

 

 

转载于:https://www.cnblogs.com/jianrenfang/p/7257531.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值