Codeforces 832D: Misha, Grisha and Underground 【LCA模板】
模板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模板】的更多相关文章
- Codeforces 832D(Misha, Grisha and Underground,LCA)
题意:在一棵生成树上,给出了三个点,求三个点之间最大的相交点数,CF难度1900. 题解:求出三个lca,并取深度最大的那个,就是我们要的三岔路口K,然后分别求出K到a,b,c三点的路径长度,取最大值 ...
- Codeforces 832D - Misha, Grisha and Underground
832D - Misha, Grisha and Underground 思路:lca,求两个最短路的公共长度.公共长度公式为(d(a,b)+d(b,c)-d(a,c))/2. 代码: #includ ...
- 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 ...
- 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) 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 ...
- 【树链剖分】【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一样无脑,但是少 ...
随机推荐
- loj#6487 基础 FFT 练习题
分析 仔细观察a[i],b[i]的数据范围 于是我们转为枚举所有a[i],b[i]的值 然后暴力即可 代码 #include<bits/stdc++.h> using namespace ...
- c# html 转Word--Spire.Doc
利用 Spire.Doc 组件,NuGet搜索“FreeSpire.Doc”有个免费版. using System;using System.Collections.Generic;using Sys ...
- leetcode 884. 两句话中的不常见单词 (python)
给定两个句子 A 和 B . (句子是一串由空格分隔的单词.每个单词仅由小写字母组成.) 如果一个单词在其中一个句子中只出现一次,在另一个句子中却没有出现,那么这个单词就是不常见的. 返回所有不常用单 ...
- mkdir: 无法创建目录"kk": 只读文件系统
创建文件提示 root@hap1:/test>mkdir kk mkdir: 无法创建目录"kk": 只读文件系统 root@hap1:/test>mount .... ...
- 刷题——有重复元素的全排列(Permutations II)
题目如上所示. 我的解决方法(参考了九章的答案!): class Solution { public: /* * @param : A list of integers * @return: A li ...
- Mac012--FinalShell SSH工具
Mac--FinalShell SSH工具 FinalShell SSH工具,服务器管理,远程桌面加速软件,支持Windows,Mac OS X,Linux FinalShell,国货产品,同类产品有 ...
- 在WebStorm中使用editorConfig插件
在webStorm中默认是支持editorConfig插件的,那么我们需要在webStorm中自定义editorConfig的配置怎么来做? 第一步:打开webStrome > File > ...
- [2019杭电多校第五场][hdu6625]three arrays(01字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6625 大意为给你两个数组a和b,对应位置异或得到c数组,现在可以将a,b数组从新排序求c数组,使得字典 ...
- python 正确复制list,克隆list 的各种方案
推荐4种方法 --------------------------------------------------------------- 方法一:extend L = [1, 2, 3] List ...
- HDU-1754 I Hate It(线段树,区间最大值)
很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师 ...