题目链接

模板copy from http://codeforces.com/contest/832/submission/28835143

题意,给出一棵有n个结点的树,再给出其中的三个结点 s,t,f ,求路径 (s,t) (s,f) (t,f) 三者的最大公共结点数

对于(t,f) (s,f)的公共结点数,有

懒得细想了,暴力对s,t,f生成排列(反正一共仨数,最多也就3!=6个排列),求各种情况下的最大ans

#include<bits/stdc++.h>
using namespace std; const int N=1e5+;
const int DEG=;
//int time_tag,in[N],out[N],dep[N];
int dep[N];
int fa[N][DEG];
int n,q;
vector<int> e[N]; void dfs(int u)
{
// in[u]=++time_tag;
for(int i=;i<DEG;i++)
fa[u][i]=fa[fa[u][i-]][i-];
for(int i=;i<e[u].size();i++)
{
int v=e[u][i];
dep[v]=dep[u]+;
fa[v][]=u;
dfs(v);
}
// out[u]=time_tag;
} int lca(int u,int v)
{
if(dep[u]<dep[v]) swap(u,v);
for(int i=,d=dep[u]-dep[v];d;i++,d>>=)
if(d&) u=fa[u][i];
if(u==v) return u;
for(int i=DEG-;i>=;i--)
if(fa[u][i]!=fa[v][i]) u=fa[u][i],v=fa[v][i];
return fa[u][];
} int dis(int u,int v)
{
return dep[u]+dep[v]-*dep[lca(u,v)];
} int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
{
int p;
scanf("%d",&p);
e[p].push_back(i);
}
dfs();
while(q--)
{
int k[];
for(int i=;i<;i++) scanf("%d",&k[i]);
sort(k,k+);
int ans=;
do
{
ans=max(ans,(dis(k[],k[])+dis(k[],k[])-dis(k[],k[]))/);
}while(next_permutation(k,k+));
printf("%d\n",ans+);
}
}

//当时其实想到了ans的表达式,但是不会LCA,不自信是否把题读准确了(前边读题出的问题很大。。),所以直接放弃了这道题。。然而事后发现这题没读错,算法也没错。。。如果直接从网上copy一份求树上两点距离的板子,这题还是可以过的——一只弱鸡的懊悔

Codeforces 832D: Misha, Grisha and Underground 【LCA模板】的更多相关文章

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

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

  2. Codeforces 832D - Misha, Grisha and Underground

    832D - Misha, Grisha and Underground 思路:lca,求两个最短路的公共长度.公共长度公式为(d(a,b)+d(b,c)-d(a,c))/2. 代码: #includ ...

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

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

  9. 【树链剖分】【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一样无脑,但是少 ...

随机推荐

  1. 高通Android camera运行流程【转】

    本文转载自:http://blog.csdn.net/unicornkylin/article/details/13293295 1.总体架构 Android Camera 框架从整体上看是一个 cl ...

  2. ContentProvider,ContentResolver和ContentObserver 使用

    1 ContentProvider内容提供者 四大组件之一,实现不同程序实现数据的共享.联系人应用就使用了ContentProvider,比如你在自己的应用可以读取和修改联系人的数据(获得相应权限). ...

  3. SynchronousQueue 源码分析

    SynchronousQueue SynchronousQueue 能解决什么问题?什么时候使用 SynchronousQueue? 1)SynchronousQueue 没有任何内部容量. 2)Sy ...

  4. javascript 跳出循环比较

    for continue 跳出当前循环,继续下一个循环 break 结束循环 forEach 不能使用continue , break return/return false 跳出当前循环,在forE ...

  5. [转载]OpenSSL身份认证 RSA、ECC、SM2

    一.生成证书openSSL生成RSA证书1 生成自签CA 生成CA密钥genrsa -aes256 -passout pass:123456 -out ca_rsa_private.pem 20481 ...

  6. 如何快速查找到多个字典中的公共键(Key)---Python数据结构与算法相关问题与解决技巧

    如何快速查找到多个字典中的公共键(Key)-?   实际案例: 西班牙足球甲级联赛,每轮球员进球统计: 第1轮: { '苏亚雷斯':1,'梅西':2,'本泽马':1,...} 第2轮: { '苏亚雷斯 ...

  7. git_01_上传第一个项目至git

    前言 Git是一个开源的分布式版本控制系统,可以有效.高速地处理从小到大的项目版本管理.最近在自己研究自动测试,也准备放到git上管理.由于工作中是在已有的代码库拉取.提交代码.自己想要初次建库上传项 ...

  8. pandas 入门(3)

    from pandas import Series, DataFrame, Index import numpy as np # 层次化索引 对数据重塑和分组操作很有用 data = Series(n ...

  9. Echats

    网址:https://www.echartsjs.com 1.特性 ECharts,一个使用 JavaScript 实现的开源可视化库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(I ...

  10. python 链接交换机并执行相关命令

    原文地址 https://blog.csdn.net/u010897775/article/details/80311786?utm_source=blogxgwz0 # encoding=utf-8 ...