Codeforces 832D Misha, Grisha and Underground【LCA】

本文介绍了一种树形结构上的查询优化方法,通过预处理倍增和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.


题目大意:


给你一棵树,每组查询三个点a,b,c,让你从中选取一个点作为终点,另外两个点作为起点,我们要求两个起点到终点的路径上,重复经过的点的个数最多,问这个最多点数。


思路:


设定S1,S2表示两个起点,T表示终点的话,如果我们能够知道S1->T S2->T的两条路径上重复覆盖的长度的话(LEN),我们就能够知道结果的点数(LEN+1)。


那么考虑求两条路径上的重复长度。


我们知道,求树上两点间距离需要求两点LCA,那么我们预处理倍增然后在线查询就行。


那么我们可以求出从S1到T的路径长度,也能求出S2到T的路径长度,那么求和后,再减去S1到LCA(S1,S2)的路径长度,再减去S2到LCA(S1,S2)的路径长度之后,得到的长度就是覆盖的重复路径长度*2,最终结果也就是这个结果/2+1。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
#include<math.h>
using namespace std;
#define N 100050
struct node
{
    int from;
    int to;
    int w;
    int next;
}e[350000];
int cont,n,m;
int p[150000][20];
int d[150000];
int dist[150000];
int head[150000];
void add(int from,int to,int w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
void Dfs(int u,int from)
{
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        if(v==from)continue;
        d[v]=d[u]+1;
        dist[v]=dist[u]+1;
        p[v][0]=u;
        Dfs(v,u);
    }
}
void init()
{
    for(int j=1;(1<<j)<=n;j++)
    {
        for(int i=1;i<=n;i++)
        {
            p[i][j]=p[p[i][j-1]][j-1];
        }
    }
}
int Lca(int x,int y)
{
    if(d[x]>d[y])swap(x,y);
    int f=d[y]-d[x];
    for(int i=0;(1<<i)<=f;i++)
    {
        if((1<<i)&f)y=p[y][i];
    }
    if(x!=y)
    {
        for(int i=(int)log2(N);i>=0;i--)
        {
            if(p[x][i]!=p[y][i])
            {
                x=p[x][i];
                y=p[y][i];
            }
        }
        x=p[x][0];
    }
    return x;
}
int Dist(int a,int b)
{
    return dist[a]+dist[b]-2*dist[Lca(a,b)];
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        cont=0;
        memset(d,0,sizeof(d));
        memset(dist,0,sizeof(dist));
        memset(head,-1,sizeof(head));
        for(int i=2;i<=n;i++)
        {
            int x;scanf("%d",&x);
            add(x,i,1);
            add(i,x,1);
        }
        Dfs(1,-1);
        init();
        while(m--)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            int ans=0;
            ans=max(Dist(b,a)+Dist(c,a)-Dist(b,Lca(b,c))-Dist(c,Lca(b,c)),ans);
            ans=max(Dist(a,b)+Dist(c,b)-Dist(a,Lca(a,c))-Dist(c,Lca(a,c)),ans);
            ans=max(Dist(a,c)+Dist(b,c)-Dist(a,Lca(a,b))-Dist(b,Lca(a,b)),ans);
            printf("%d\n",ans/2+1);
        }
    }
}







(Kriging_NSGA2)克里金模型结合多目标遗传算法求最优因变量及对应的最佳自变量组合研究(Matlab代码实现)内容概要:本文介绍了克里金模型(Kriging)与多目标遗传算法NSGA-II相结合的方法,用于求解最优因变量及其对应的最佳自变量组合,并提供了完整的Matlab代码实现。该方法首先利用克里金模型构建高精度的代理模型,逼近复杂的非线性系统响应,减少计算成本;随后结合NSGA-II算法进行多目标优化,搜索帕累托前沿解集,从而获得多个最优折衷方案。文中详细阐述了代理模型构建、算法集成流程及参数设置,适用于工程设计、参数反演等复杂优化问题。此外,文档还展示了该方法在SCI一区论文中的复现应用,体现了其科学性与实用性。; 适合人群:具备一定Matlab编程基础,熟悉优化算法和数值建模的研究生、科研人员及工程技术人员,尤其适合从事仿真优化、实验设计、代理模型研究的相关领域工作者。; 使用场景及目标:①解决高计算成本的多目标优化问题,通过代理模型降低仿真次数;②在无法解析求导或函数高度非线性的情况下寻找最优变量组合;③复现SCI高水平论文中的优化方法,提升科研可信度与效率;④应用于工程设计、能源系统调度、智能制造等需参数优化的实际场景。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现过程,重点关注克里金模型的构建步骤与NSGA-II的集成方式,建议自行调整测试函数或实际案例验证算法性能,并配合YALMIP等工具包扩展优化求解能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值