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的更多相关文章

  1. Codeforces 832D: Misha, Grisha and Underground 【LCA模板】

    题目链接 模板copy from http://codeforces.com/contest/832/submission/28835143 题意,给出一棵有n个结点的树,再给出其中的三个结点 s,t ...

  2. Codeforces 832D(Misha, Grisha and Underground,LCA)

    题意:在一棵生成树上,给出了三个点,求三个点之间最大的相交点数,CF难度1900. 题解:求出三个lca,并取深度最大的那个,就是我们要的三岔路口K,然后分别求出K到a,b,c三点的路径长度,取最大值 ...

  3. 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 ...

  4. Codeforces 832 D Misha, Grisha and Underground

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

  5. 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 ...

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

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

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

  9. 【 Codeforces Round #425 (Div. 2) D】Misha, Grisha and Underground

    [Link]:http://codeforces.com/contest/832/problem/D [Description] 给你一棵树; 然后给你3个点 让你把这3个点和点s,t,f对应; 然后 ...

随机推荐

  1. Docker深入浅出2

    Docker系统架构 Docker使用客户端-服务端(c/s)架构模式,使用远程api来管理和创建Docker容器. docker容器通过Docker镜像来创建. 容器与镜像的关系类似于面向对象编程中 ...

  2. FRM-40212: Invalid value for field %s.

    Cause:        Caused by one of the following: 1.  The value is not of the proper data type. 字段类型不对 2 ...

  3. Amaze UI JS 气泡弹出

    http://amazeui.org/javascript/popover?_ver=2.x

  4. 5select的运用

    四.select的运用 --汇总函数 max()最大值,min()最小值,avg()平均值select max(age),min(age),avg(age) from tablename; --算出表 ...

  5. Linux虚拟机克隆后网卡UUID问题

    虚拟机中的Linux系统克隆后,网卡配置eth0中的UUID可被克隆的系统是一样的,这样UUID就失去了唯一性. 我参考了该篇博客: 有时我们不小心将/etc/sysconfig/network-sc ...

  6. quartz开源作业调度框架的配置

    quartz开源作业调度框架的job服务实现,Quartz是一个完全由java编写的开源作业调度框架,使用时候需要创建一个实现org.quartz.Job接口的java类,Job接口包含唯一的方法: ...

  7. Linux基础命令---mke2fs

    mke2fs 在磁盘分区上创建ext2.ext3.ext4文件系统,默认情况下会创建ext2.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedo ...

  8. php判断数组元素是否存在某个字符串的方法

    php判断数组元素是否存在某个字符串的方法: 方法一:采用in_array(value,array,type) type 可选.如果设置该参数为 true,则检查搜索的数据与数组的值的类型是否相同. ...

  9. 2018 Java线程热门面试题,你知道多少?

    面试,难还是不难?取决于面试者的底蕴(气场+技能).心态和认知及沟通技巧.面试其实可以理解为一场聊天和谈判,在这过程中有心理.思想上的碰撞和博弈.其实你只需要搞清楚一个逻辑:“面试官为什么会这样问?他 ...

  10. Docker与虚拟机技术

    最近docker技术在网络上非常火爆,各种技术下载中心总能看到一个以docker镜像方式下载的下载选项,而当你下载下来运行发现,这就是一个虚拟机嘛.究竟是不是呢?一起来看看. 我们先来看看传统意义上的 ...