有一棵n个节点的树,一共q 次询问

每次询问给定3个点,求两条起点终点在这三个点上且不完全重合的路径的最多公共节点数


简单LCA求距离,令a为汇合点,那么答案就是(dis(a,b) + dis(a,c) - dis(b,c)) / 2 + 1,dis用lca求出,枚举a就好。

当然也可以一一讨论abc的位置关系,不过容易出错。

#include<queue>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=2e5+10;
#define int long long
int Next[N*2],head[N],go[N*2],tot;
inline void add(int u,int v){
Next[++tot]=head[u];head[u]=tot;go[tot]=v;
Next[++tot]=head[v];head[v]=tot;go[tot]=u;
}
int n,q;
int d[N],f[N][21];
inline void pre(int u,int fa){
d[u]=d[fa]+1;f[u][0]=fa;
for(int i=1;i<=20;i++)f[u][i]=f[f[u][i-1]][i-1];
for(int i=head[u];i;i=Next[i]){
int v=go[i];
if(v==fa)continue;
pre(v,u);
}
}
inline int LCA(int x,int y){
if(d[x]<d[y])swap(x,y);
for(int i=20;i>=0;i--)if(d[f[x][i]]>=d[y])x=f[x][i];
if(x==y)return x;
for(int i=20;i>=0;i--)if(f[x][i]!=f[y][i])x=f[x][i],y=f[y][i];
return f[x][0];
}
inline int dis(int a,int b){
int c=LCA(a,b);
return d[a]-d[c]+d[b]-d[c];
}
inline int ask(int a,int b,int c){
return 1+(dis(a,b)+dis(a,c)-dis(b,c))>>1;
}
signed main(){
cin>>n>>q;
for(int i=2,x;i<=n;i++)
scanf("%lld",&x),add(x,i);
pre(1,0);
int a,b,c;
while(q--){
scanf("%lld%lld%lld",&a,&b,&c);
int ans=0;
ans=max(ans,ask(a,b,c));
ans=max(ans,ask(b,a,c));
ans=max(ans,ask(c,b,a));
printf("%lld\n",ans);
}
}

CF832D Misha, Grisha and Underground的更多相关文章

  1. Codeforces 832D - Misha, Grisha and Underground

    832D - Misha, Grisha and Underground 思路:lca,求两个最短路的公共长度.公共长度公式为(d(a,b)+d(b,c)-d(a,c))/2. 代码: #includ ...

  2. 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 inpu ...

  3. Codeforces 832 D Misha, Grisha and Underground

    Misha, Grisha and Underground 题意:Misha 和 Grisha 是2个很喜欢恶作剧的孩子, 每天早上 Misha 会从地铁站 s 通过最短的路到达地铁站 f, 并且在每 ...

  4. Codeforecs Round #425 D Misha, Grisha and Underground (倍增LCA)

    D. Misha, Grisha and Underground time limit per test 2 seconds memory limit per test 256 megabytes i ...

  5. D. Misha, Grisha and Underground 树链剖分

    D. Misha, Grisha and Underground 这个题目算一个树链剖分的裸题,但是这个时间复杂度注意优化. 这个题目可以选择树剖+线段树,时间复杂度有点高,比较这个本身就有n*log ...

  6. Misha, Grisha and Underground CodeForces - 832D (倍增树上求LCA)

    Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...

  7. Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组

    Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...

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

    我奇特的脑回路的做法就是 树链剖分 + 树状数组 树状数组是那种 区间修改,区间求和,还有回溯的 当我看到别人写的是lca,直接讨论时,感觉自己的智商收到了碾压... #include<cmat ...

  9. 【树链剖分】【dfs序】【LCA】【分类讨论】Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground

    一棵树,q次询问,每次给你三个点a b c,让你把它们选做s f t,问你把s到f +1后,询问f到t的和,然后可能的最大值是多少. 最无脑的想法是链剖线段树……但是会TLE. LCT一样无脑,但是少 ...

随机推荐

  1. C#中的取整函数

    先放百度的 Math.Ceiling();向上取整 Math.Ceiling()向上取整: d = 4.56789 string res = Math.Ceiling(Convert.ToDecima ...

  2. javascript 字符串转化成函数执行

    javascript 字符串转化成函数执行<pre>function func_abc(){ alert('a');}var str = "func_abc";eval ...

  3. SpringMVC错误:nested exception is java.lang.IncompatibleClassChangeError: class org.springframework.core.type.clas

    这是jar包冲突引起的 spring-core.jar已经有asm 所以不用再单独导入asm包了

  4. Docker学习-Spring Boot on Docker

    1.创建spring boot项目 https://start.spring.io/ pom.xml文件新增docker支持 <build> <plugins> <plu ...

  5. C# II: Class ViewModelBase and RelayCommand in MVVM

    好久不写WPF和MVVM,新建一个Project后,想起来ViewModelBase和RelayCommand没有.以下Code摘自MSDN上的Article:Patterns - WPF Apps ...

  6. Python连接SqlServer+GUI嵌入式——学生管理系统1.0

    学生管理系统1.0 1.建学生数据库 2.数据库嵌入高级语言(Python) 3.界面设计 简化思路: 1.先通过SqlServer2012建立学生数据库,包括账号.密码,姓名.选课等信息 2.运用P ...

  7. [LC]876题 Middle of the Linked List (链表的中间结点)(链表)

    ①中文题目 给定一个带有头结点 head 的非空单链表,返回链表的中间结点. 如果有两个中间结点,则返回第二个中间结点. 示例 1: 输入:[1,2,3,4,5]输出:此列表中的结点 3 (序列化形式 ...

  8. 【SQL】安装后设置 sa账号登录

    1.windows身份验证登录管理员账户   2.找到sa账号双击  3.设置密码,启用 4.右键 属性 安全性   5.重启服务,sa账号登录   本地服务用  .     6.登录成功:

  9. 通过C/C++,实现一元一次方程求解

    通过C/C++,实现一元一次方程求解: #include <bits/stdc++.h> using namespace std; string str, str_l, str_r; st ...

  10. HashSet源码学习,基于HashMap实现

    HashSet源码学习 一).Set集合的主要使用类 1). HashSet 基于对HashMap的封装 2). LinkedHashSet 基于对LinkedHashSet的封装 3). TreeS ...