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一样无脑,但是少 ...
随机推荐
- Docker 学习 | 基础命令
基本概念定义 基本组成 客户端/守护进程 C/S架构 本地/服务器 镜像 容器基石 只读文件系统 联合加载(union mount) 容器 通过镜像启动 执行 写时复制 仓库 公有 docker hu ...
- Maven/Docker快速搭建RocketMQ
官方文档 [https://rocketmq.apache.org/docs/quick-start/] ①:Bin_二进制安装版 1. 环境准备 系统环境:Centos7 x64 JDK:jdk-8 ...
- Magicodes.Pay,打造开箱即用的统一支付库,已提供ABP模块封装
Magicodes.Pay,打造开箱即用的统一支付库,已提供ABP模块封装 简介 Magicodes.Pay,是心莱科技团队提供的统一支付库,相关库均使用.NET标准库编写,支持.NET Framew ...
- 从EFCore上下文的使用到深入剖析DI的生命周期最后实现自动属性注入
故事背景 最近在把自己的一个老项目从Framework迁移到.Net Core 3.0,数据访问这块选择的是EFCore+Mysql.使用EF的话不可避免要和DbContext打交道,在Core中的常 ...
- Xshell选中的同时把内容复制到剪贴板
1.设置对话框 工具 -> 选项 -> 键盘和鼠标 -> 将选定的文本自动复制到剪贴板 2.贴图如下 2.1.打开设置对话框 2.2.设置键盘鼠标,左键复制
- ASP.NET Core 1.0: Using Entity Framework Core
伴随着ASP.NET Core 1.0发布的还有Entity Framework Core 1.0; 官方文档链接:https://docs.efproject.net/en/latest/platf ...
- 一、netcore跨平台之 Linux上部署netcore和webapi
这几天闲着的时候在linux上部署了一下netcore webapi,下面就纪要一下这个过程. 中间遇到不少的坑,心里都是泪啊. 话不多说,开始干活. ------------------------ ...
- CentOS7安装PPTP
CentOS7安装PPTP VPN(开启firewall防火墙) 1 准备一个CentOS7服务器 2 检查是否支持PPTP && echo ok #返回OK ...
- pat 1116 Come on! Let's C(20 分)
1116 Come on! Let's C(20 分) "Let's C" is a popular and fun programming contest hosted by t ...
- 小程序api的promise封装
微信小程序和支付宝小程序的api封装方法是一样的,都是外部新建一个js,使用module.exports导出,要注意的是,最好使用post请求,虽然get请求没什么不好,主要是好修改.这里使用的MD5 ...