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. malloc 实现原理

    1. Reference: 如何实现一个malloc http://blog.codinglabs.org/articles/a-malloc-tutorial.html 2.

  2. BinarySearch

    今天看代码,看到这么一段,开始没有看明白,记录下来备忘 foreach (FinancialReport r3 in addAorList)            {                i ...

  3. MySQL从删库到跑路_高级(二)——自定义函数

    作者:天山老妖S 链接:http://blog.51cto.com/9291927 一.自定义函数简介 自定义函数(user-defined function UDF)是一种对MySQL扩展的途径,其 ...

  4. swift 之 as、as!、as?

    1,as使用场合(1)从派生类转换为基类,向上转型(upcasts) class Animal {} class Cat: Animal {} let cat = Cat() let animal = ...

  5. Nuget的学习总结

    Nuget的学习总结 今天研究了一下nuget,发现nuget实在是太有用了,便写下了这篇博客,希望记录一下自己的学习历程,也希望技术圈的朋友看到之后,如果里面哪里写的不够好,可以给我些宝贵的意见,以 ...

  6. json-lib基础

    一.json-lib所需的jar包: json-lib.jar,commons-beanutils.jar,commons-collections.jar,commons-lang.jar,commo ...

  7. eclispe 相关设置

    1. 关闭js文件校验: 1). windows->preference->Java Script->Validator->Errors/Warnings->Enable ...

  8. Linux服务器---关闭selinux

    关闭selinux 1.通过命令“getenforce”获取selinux状态, [root@localhost ~]# getenforce Enforcing        //enforcein ...

  9. 比特币、莱特币钱包下载和把数据迁移到C盘以外其他盘

    比特币是目前最热门和价格最高的虚拟币,国内外多个平台可以进行交易,有些商家可以用比特币进行支付有些国家可以在ATM取款. Bitcoin-Qt就是最早的比特币客户端,构建了比特币的骨干网络,具有高度的 ...

  10. JavaScript Image对象 / Tabel对象 / Select对象 / Form对象

    JavaScript Image / Tabel / Select / Form 对象 版权声明:未经授权,严禁转载! Image 对象 Image 对象,代表 <img> 元素. < ...