CF832D Misha, Grisha and Underground
有一棵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的更多相关文章
- Codeforces 832D - Misha, Grisha and Underground
832D - Misha, Grisha and Underground 思路:lca,求两个最短路的公共长度.公共长度公式为(d(a,b)+d(b,c)-d(a,c))/2. 代码: #includ ...
- 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 ...
- Codeforces 832 D Misha, Grisha and Underground
Misha, Grisha and Underground 题意:Misha 和 Grisha 是2个很喜欢恶作剧的孩子, 每天早上 Misha 会从地铁站 s 通过最短的路到达地铁站 f, 并且在每 ...
- 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 ...
- D. Misha, Grisha and Underground 树链剖分
D. Misha, Grisha and Underground 这个题目算一个树链剖分的裸题,但是这个时间复杂度注意优化. 这个题目可以选择树剖+线段树,时间复杂度有点高,比较这个本身就有n*log ...
- 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 ...
- 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 ...
- Codeforces Round #425 (Div. 2) D.Misha, Grisha and Underground
我奇特的脑回路的做法就是 树链剖分 + 树状数组 树状数组是那种 区间修改,区间求和,还有回溯的 当我看到别人写的是lca,直接讨论时,感觉自己的智商收到了碾压... #include<cmat ...
- 【树链剖分】【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一样无脑,但是少 ...
随机推荐
- Java Part 001( 01_01_Java概述 )
Java作为编程语言, 甚至超出了语言的范畴, 成为一种开发平台, 一种开发规范. Java语言相关的JavaEE规范里, 包含了时下最流行的各种软件工程理念, 学习Java相当于系统的学习了软件开发 ...
- 使用ASP.NET Core 3.x 构建 RESTful API - 3.1 资源命名
之前讲了RESTful API的统一资源接口这个约束,里面提到了资源是通过URI来进行识别的,每个资源都有自己的URI.URI里还涉及到资源的名称,而针对资源的名称却没有一个标准来进行规范,但是业界还 ...
- 对于 TCP 三次握手的理解
假设名叫 A 和 B 的两个人要进行通信,那么他们两人之间,首先要确保通信顺畅. 而确保通信顺畅,就要从 3 个维度,确定 8 个能力 3 个维度分别是: 1.人知道(A 知道.B 知道) 2.人(A ...
- JavaScript with Image:创建缩略图
当图片很大,直接把图片从Server下载到浏览器上看是一种很不明智的做法,浪费了服务器的资源,网络带宽和客户端的资源.所以,通常Server和Client之间会传输缩略图,只有当Client请求某张图 ...
- C++中对C的扩展学习新增内容———面向对象(封装)
面向对象(封装) 1.对封装的理解: 1.封装就是把变量和函数放在一起统一表示某一个食物. class 2.给类内部的成员增加访问控制权限. 3.封装的语法就是class定义一个类. 2.给对象成员增 ...
- P4-verilog实现mips单周期CPU
最近对学习的掌控可能出现了问题,左支右绌,p2挂了,p2.p3.p4.p5每周在计组花的连续时间少了很多,学习到的东西也少了很多,流水线都还没真正开始写,和别人比落后了一大截,随笔自然就荒废了,我得尽 ...
- 技术人如何利用 github+Jekyll ,搭建一个独立免费的技术博客
上次有人留言说,技术博客是程序员的标配,但据我所知绝大部分技术同学到现在仍然没有自己的技术博客.原因有很多,有的是懒的写,有的是怕写不好,还有的是一直想憋个大招,幻想做到完美再发出来,结果一直胎死腹中 ...
- Kotlin Coroutines不复杂, 我来帮你理一理
Coroutines 协程 最近在总结Kotlin的一些东西, 发现协程这块确实不容易说清楚. 之前的那篇就写得不好, 所以决定重写. 反复研究了官网文档和各种教程博客, 本篇内容是最基础也最主要的内 ...
- Previous operation has not finished; run 'cleanup' if it was interrupted最简单有效的解决方法
今天提交代码报错,看了看提示的错误,百度了一下,发现操作都比较繁琐,所以自己重新给一个最简单有效的. 有的要下载sqlite3.exe,借助它清空本地.svn\wc.db数据库文件里面的operati ...
- java变量与常量
常量: 定义:程序运行过程中,不能再次该表的指 作用: 1.固定的值,代表计算过程中经常用到的值,便于计算 2.用来代表一个含义 键盘:8代表up 4代表left 6代表right 5代表down ...