Codeforces 832D - Misha, Grisha and Underground
832D - Misha, Grisha and Underground
思路:lca,求两个最短路的公共长度。公共长度公式为(d(a,b)+d(b,c)-d(a,c))/2。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ls rt<<1,l,m
#define rs rt<<1|1,m+1,r
const int INF=0x3f3f3f3f;
const int N=1e5+;
const int logn=;
vector<int>g[N];
int anc[logn][N];
int deep[N];
void dfs(int o,int u)
{
deep[u]=deep[o]+;
for(int j=;j<g[u].size();j++)
{
if(g[u][j]!=o)
{
anc[][g[u][j]]=u;
for(int i=;i<logn;i++)anc[i][g[u][j]]=anc[i-][anc[i-][g[u][j]]];
dfs(u,g[u][j]);
}
}
}
int lca(int u,int v)
{
if(deep[u]<deep[v])swap(u,v);
for(int i=logn-;i>=;i--)if(deep[anc[i][u]]>=deep[v])u=anc[i][u];
if(u==v)return u;
for(int i=logn-;i>=;i--)if(anc[i][u]!=anc[i][v])u=anc[i][u],v=anc[i][v];
return anc[][u];
}
int dis(int u,int v)
{
int l=lca(u,v);
return deep[u]+deep[v]-*deep[l];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int n,q;
cin>>n>>q;
for(int i=;i<=n;i++)
{
int a;
cin>>a;
g[a].push_back(i);
g[i].push_back(a);
}
for(int i=;i<logn;i++)anc[i][]=;
dfs(,);
while(q--)
{
int a,b,c;
cin>>a>>b>>c;
int d1=(dis(a,b)+dis(b,c)-dis(a,c))/+;
int d2=(dis(a,c)+dis(b,c)-dis(a,b))/+;
int d3=(dis(a,b)+dis(a,c)-dis(b,c))/+;
int ans=max(d1,max(d2,d3));
cout<<ans<<endl;
}
return ;
}
Codeforces 832D - Misha, Grisha and Underground的更多相关文章
- Codeforces 832D: Misha, Grisha and Underground 【LCA模板】
题目链接 模板copy from http://codeforces.com/contest/832/submission/28835143 题意,给出一棵有n个结点的树,再给出其中的三个结点 s,t ...
- Codeforces 832D(Misha, Grisha and Underground,LCA)
题意:在一棵生成树上,给出了三个点,求三个点之间最大的相交点数,CF难度1900. 题解:求出三个lca,并取深度最大的那个,就是我们要的三岔路口K,然后分别求出K到a,b,c三点的路径长度,取最大值 ...
- 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 ...
- 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 ...
- 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) D】Misha, Grisha and Underground
[Link]:http://codeforces.com/contest/832/problem/D [Description] 给你一棵树; 然后给你3个点 让你把这3个点和点s,t,f对应; 然后 ...
随机推荐
- DW课堂练习 用所学的知识去制作一个 (邮箱的注册页面)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- linux中的各种$号 位置参数变量
位置参数变量 $n #/bin/bash echo $0(代表命令本身); echo $1; (代表第几个参数) echo $2; [root@LocalWeb01 ~]# ./1.sh ...
- linux服务后台管理
把进程放到后台有两种方法 1.cmmand & 2.ctrl+z 暂停到后台 查看后台服务 jobs 把后台进程移到前台 fg %2 工作号 恢复到前台 后台服务继续执行 bg ...
- mybatis例子
mybatis的mapper不允许重载,因为它需要通过方法名称[不加签名]去查找需要执行的sql 1.批量删除 <delete id="deletePlanLocations" ...
- thymeleaf 配置
thymeleaf,官网文档中,那个配置有误(估计是代码更新了但是文档没有更新).应该是这样的- <bean id="templateResolver" class=&quo ...
- linux常用命令:whereis 命令
whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 和find相比,whereis查找的速度非 ...
- JVM内存分布
参考引用文章地址: http://hllvm.group.iteye.com/group/wiki/3053-JVMhttp://blog.csdn.net/william001zs/article/ ...
- SpringBoot之集成Socket
1.Socket是什么,这里不做介绍.开发环境:jdk1.8,win7_64旗舰版,idea 2.初始化一个springboot项目 3.开始Socket服务端实现,Socket相关接口在 ...
- C/C++中的位运算
位运算 位运算的运算分量只能是整型或字符型数据,位运算把运算对象看作是由二进位组成的位串信息,按位完成指定的运算,得到位串信息的结果. 位运算符有: &(按位与).|(按位或) ...
- Python Web学习笔记之GIL机制下的鸡肋多线程
为什么有人会说 Python 多线程是鸡肋?知乎上有人提出这样一个问题,在我们常识中,多进程.多线程都是通过并发的方式充分利用硬件资源提高程序的运行效率,怎么在 Python 中反而成了鸡肋? 有同学 ...