题目链接

模板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. Linux内核调试方法总结之内核通知链

    Linux内核通知链notifier 1.内核通知链表简介(引用网络资料)    大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣.为了满足这个需求,也即是让某个子系统在 ...

  2. MySQL高可用方案 MHA之一MHA安装

    MHA0.58安装 MHA(Master High Availability)由两部分组成:MHA Manager(管理节点)和MHA Node(数据节点).管理节点mha4mysql-manager ...

  3. python-笔记(四)函数

    一.函数是什么? 函数一次来源于数学,但是编程中的[函数]的概念,与数学中的函数还是有很大的不同的,编程中的函数在英文中也有很多不同的叫法. 在Basic中叫做subroutine(子过程或子程序), ...

  4. Navicat Premium for Mac 非官方版不能启动的解决方案

    Ps:这篇有点杂记的感觉,就说点废话也没什么影响.废话主要有两点: 1.建议读者也开始写博客,为什么呢?其实我也没有这种写作的习惯,我最开始写博客的时候,感觉我写的东西网上都有,需要的时候找一下肯定能 ...

  5. vue 组件传参

    路由配好了 再传个参呗 注:组件信息流转的时候只能单向1 > 父子传参传参:通过属性prop:传递数据 a.父组件传参给子组件 子组件: <ul> <li v-for=&quo ...

  6. pandas入门(1)

    import pandas as pd import numpy as np # 自动创建索引 obj = pd.Series([4, 7, -5, 2]) print(obj, type(obj)) ...

  7. JAVA的学习

    怎么说呢,我已经接触JAVA已经两周了,个人感觉还是不懂,哈哈,JAVA是一门编程语言,是大多数开发者较为习惯的编程模式,我感觉相对比C语言来说可能简单学点,可能是我先接触C语言把,或许因人而异把,在 ...

  8. Mysql UPF 安装文档

    一.mysql UDF 简介: github地址: http://www.mysqludf.org/lib_mysqludf_preg 二.mysql UDF 下载地址: https://github ...

  9. HDFS镜像文件fsimage和编辑日志文件edits

    镜像文件和编辑日志文件 1)概念 namenode被格式化之后,将在/opt/module/hadoop-2.7.2/data/tmp/dfs/name/current目录中产生如下文件 edits_ ...

  10. sublime text3插件安装及使用

    sublime官网下载地址:http://www.sublimetext.com/ 我用的是SubLime text3,插件安装命令地址:https://packagecontrol.io/insta ...