cf 832d

http://www.elijahqi.win/archives/384
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 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 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一个变量名错了,又调试半天啊

题意:给三个点,问三个点任选两个到另一个点重合点数最多

三选二嘛,一共三种情况依次枚举一下

设l1=lca(s,f),l2=lca(t,f)l3=lca(s,t)

用我们f点的深度减去l1,l2中深度最深的点+1就是我们的答案,当然这只是他们相交后半段的,加入前半段也有交点,那么他们l1,l2一定相同,把答案加上lca(s,t)的深度减去l1的深度

或者l2也行,因为两者相同。这个是基于下面合并而成的,减少了lca次数应该快一些的

那么分为以下几种情况:

1、l1==f,说明s在f的子树里。同理l2==f,说明t在f的子树里。如果s,t都在f的子树里,那么重复的路径显然就是(l3,f)。

2、如果只有一个在f的子树里,另一个不在,那么就只可能有f一个点重复。

3、如果s,t都不在f的子树里,那么我们把(s,f)分为(s,l1)(l1,f),(t,f)分为(t,l2)(l2,f)。如果l1==l2,那么(l1,f)上的点一定是重复的。那前半段有没有可能也有重复呢?有,也就是(l3,l1).

4、如果l1!=l2,我们假设l1深度更大,那么(l1,f)的路径是重复的,前半段不可能有重复的。(否则l1==l2)

#include<cstdio>
#define N 110000
inline int read(){
    int x=0;char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
struct node{
    int y,next;
}data[N<<1];
int Log[N],fa[N][20],low[N],h[N],n,q,num;
inline int max(int x,int y){
    return x>y?x:y;
}
inline void swap(int &x,int &y){
    x^=y;y^=x;x^=y;
}
inline void dfs(int x){
    for (int i=1;i<=Log[low[x]];++i) fa[x][i]=fa[fa[x][i-1]][i-1];
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;
        if (y==fa[x][0])continue;low[y]=low[x]+1;
        fa[y][0]=x;dfs(y);
    }
}
inline int lca(int x,int y){
    if (low[x]<low[y]) swap(x,y);
    int dis=low[x]-low[y];
    for (int i=0;i<=Log[dis];++i) if(dis&(1<<i)) x=fa[x][i];
    if (x==y) return x;
    for (int i=Log[n];i>=0;--i) 
    if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
    return fa[x][0];
}
inline int calc(int s,int f,int t){
    int l1=lca(s,f),l2=lca(t,f);
    int ans=low[f]-max(low[l1],low[l2])+1;
    if (l1==l2) ans+=low[lca(s,t)]-low[l1];
    return ans;
}
int main(){
    freopen("cf.in","r",stdin);
    //freopen("cf.out","w",stdout);
    n=read();q=read();
    Log[0]=-1;
    for (int i=1;i<=n;++i) Log[i]=Log[i>>1]+1;
    for (int i=2;i<=n;++i){
        int y=read();data[++num].y=y;data[num].next=h[i];h[i]=num;
        data[++num].y=i;data[num].next=h[y];h[y]=num;
    }
    dfs(1);
    /*for (int i=1;i<=n;++i){
        for (int j=0;j<=5;++j) printf("%d ",fa[i][j]);printf("\n");
    }*/
    int s,f,t,tmp;
    for (int i=1;i<=q;++i){
        s=read();f=read();t=read();tmp=0;
        tmp=max(calc(s,f,t),calc(f,t,s));
        printf("%d\n",max(tmp,calc(t,s,f)));
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值