codeforces 832D——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 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 ≤ 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.



给一棵树,三个点,问其中两个点到第三个点的公共路径长度最大值是多少。

三个点两两求一下LCA,肯定会有两个LCA相同。然后再分一下情况求距离即可。




#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAXN=400100;
const int mod=1e9+7;
struct edge{
    int to,next;
}e[MAXN<<1];
int head[MAXN];
int tot=0;
void add(int u,int v){
    e[tot].to=v;
    e[tot].next=head[u];
    head[u]=tot++;
}

int dep[MAXN];
int fir[MAXN];
int que[MAXN<<1];
int p[MAXN];
int st[MAXN][20];
int vis[MAXN];
int cnt=0;

void dfs(int u,int d){
    vis[u]=1;
    que[++cnt]=u;fir[u]=cnt;dep[cnt]=d;
    for(int k=head[u];k!=-1;k=e[k].next){
        int v=e[k].to;
        if(!vis[v]){
            p[v]=u;
            dfs(v,d+1);
            que[++cnt]=u;dep[cnt]=d;
        }
    }
}
void ST(int n){
    for(int i=1;i<=n;i++)
        st[i][0]=i;
    for(int j=1;(1<<j)<=n;j++){
        for(int i=1;i+(1<<j)-1<=n;i++){
            int a=st[i][j-1],b=st[i+(1<<(j-1))][j-1];
            st[i][j]=dep[a]<dep[b]?a:b;
        }
    }
}
int RMQ(int l,int r){
    int k=0;
    while((1<<(k+1))<=r-l+1){
        k++;
    }
    int a=st[l][k],b=st[r-(1<<k)+1][k];
    return dep[a]<dep[b]?a:b;
}

int lca(int u,int v){
    int x=fir[u],y=fir[v];
    if(x>y)
        swap(x,y);
    int res=RMQ(x,y);
    return que[res];
}



struct path{
    int u,v;
    int lca;
}pa[MAXN];

int cmp(path x,path y){
    return dep[fir[x.lca]]>dep[fir[y.lca]];
}
int used[MAXN];
void init(){
    tot=0;
    cnt=0;
    p[1]=0;
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    memset(used,0,sizeof(used));
}

int depth(int u){
    return -dep[fir[u]];
}
int L[3];
int main(){
    int n,q;
    while(scanf("%d%d",&n,&q)!=EOF){
        init();
        for(int i=2;i<=n;i++){
            int u;
            scanf("%d",&u);
            add(i,u);
            add(u,i);
        }
        dfs(1,1);
        ST(2*n-1);
        for(int i=0;i<q;i++){
            int a,b,c;
            int res=0;
            scanf("%d%d%d",&a,&b,&c);
            L[0]=lca(a,b);
            L[1]=lca(a,c);
            L[2]=lca(b,c);
            if(L[0]==L[1]&&L[0]==L[2]){
                res=max(res,depth(L[0])-depth(a));
                res=max(res,depth(L[0])-depth(b));
                res=max(res,depth(L[0])-depth(c));
            }else{
                if(L[1]==L[2]){
                    swap(a,c);
                    swap(L[0],L[2]);
                }else if(L[0]==L[2]){
                    swap(a,b);
                    swap(L[1],L[2]);
                }
                res=max(res,2*depth(L[0])-depth(a)-depth(L[2]));
                if(L[2]==b){
                    res=max(res,depth(b)-depth(c));
                }else if(L[2]==c){
                    res=max(res,depth(c)-depth(b));
                }else
                    res=max(res,max(depth(L[2])-depth(b),depth(L[2])-depth(c)));
            }
            printf("%d\n",res+1);
        }
    }
}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值