D. Misha, Grisha and Underground
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations connected with n - 1 routes so that each route connects two stations, and it is possible to reach every station from any other.

The boys decided to have fun and came up with a plan. Namely, in some day in the morning Misha will ride the underground from station sto station f by the shortest path, and will draw with aerosol an ugly text "Misha was here" on every station he will pass through (including sand f). After that on the same day at evening Grisha will ride from station t to station f by the shortest path and will count stations with Misha's text. After that at night the underground workers will wash the texts out, because the underground should be clean.

The boys have already chosen three stations ab and c for each of several following days, one of them should be station s on that day, another should be station f, and the remaining should be station t. They became interested how they should choose these stations sft so that the number Grisha will count is as large as possible. They asked you for help.

Input

The first line contains two integers n and q (2 ≤ n ≤ 105, 1 ≤ q ≤ 105) — the number of stations and the number of days.

The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi ≤ n). The integer pi means that there is a route between stations pi and i. It is guaranteed that it's possible to reach every station from any other.

The next q lines contains three integers ab and c each (1 ≤ a, b, c ≤ n) — the ids of stations chosen by boys for some day. Note that some of these ids could be same.

Output

Print q lines. In the i-th of these lines print the maximum possible number Grisha can get counting when the stations st and f are chosen optimally from the three stations on the i-th day.

题意:

  对于一个终点站f, 从s出发,和从t出发。经过的相同站有多少 

  有n个点,n-1条边。m次询问给出3个数 a, b, c。在a, b, c中选择一个终点两个起点,使得共同的站最大。

题解:

  由于是一个树,所以就直接建树(点到点的距离设置为1)。暴力跑a,b, c分别为终点的情况。

  现在讨论a 为顶点的情况。

  

  那么公共的站点就是公共距离+1。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
const LL INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int maxn = +;
const int DEG = ;
struct node
{
int to, next;
int w;
}edge[*maxn];
int head[maxn], tol, flag[maxn];
void init() {
tol = ;
ms(head, -);
ms(flag, );
}
void addedge(int u, int v, int w)
{
edge[tol].to = v;
edge[tol].next = head[u];
edge[tol].w = w;
head[u] = tol++;
}
int fa[maxn][DEG];
int DD[maxn];
int deg[maxn];
void bfs(int root)
{
queue<int> que;
deg[root] = ;
fa[root][] = root;
DD[root] = ;
que.push(root);
while(!que.empty()){
int tmp = que.front();
que.pop();
for(int i = ;i<DEG;i++){
fa[tmp][i] = fa[fa[tmp][i-]][i-];
}
for(int i = head[tmp];i!=-;i=edge[i].next){
int v = edge[i].to;
if(v == fa[tmp][]) continue;
deg[v] = deg[tmp]+;
fa[v][] = tmp;
DD[v] = DD[tmp]+edge[i].w;
que.push(v);
}
}
}
int LCA(int u, int v){
int ans = ;
if(deg[u]>deg[v]) swap(u, v);
int hu = deg[u], hv = deg[v];
int tu = u, tv = v;
for(int det = hv - hu, i=;det;det>>=, i++){
if(det&){
tv = fa[tv][i];
}
}
if(tu==tv){
return tu;
}
for(int i = DEG - ;i>=;i--){
if(fa[tu][i] == fa[tv][i]) continue;
tu = fa[tu][i];
tv = fa[tv][i];
}
return fa[tu][];
}
int DIS(int u, int v)
{
return DD[u] + DD[v] - *DD[LCA(u, v)];
}
void solve()
{
int n, q;
cin >> n >> q;
for(int i = ;i<=n;i++){
int x;cin >> x;
addedge(i, x, );
addedge(x, i, );
flag[x] = ;
}
int root;
for(int i = ;i<=n;i++){
if(!flag[i]){
root = i;
break;
}
}
// cout << root << endl;
bfs(root);
for(int i = ;i<q;i++){
int a, b, c;
cin >> a >> b >> c;
int ans = ;
ans = max(ans, (DIS(b, a)+DIS(c, a)-DIS(b, c))/);
ans = max(ans, (DIS(a, b)+DIS(c, b)-DIS(a, c))/);
ans = max(ans, (DIS(a, c)+DIS(b, c)-DIS(a, b))/);
cout << ans+ << endl;
}
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio();
cin.tie();
init();
solve();
}

Codeforecs Round #425 D Misha, Grisha and Underground (倍增LCA)的更多相关文章

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

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

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

  3. Codeforces 832D - Misha, Grisha and Underground

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

  4. Codeforces 832 D Misha, Grisha and Underground

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

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

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

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

  7. Codeforces Round #425 (Div. 2) D.Misha, Grisha and Underground

    我奇特的脑回路的做法就是 树链剖分 + 树状数组 树状数组是那种 区间修改,区间求和,还有回溯的 当我看到别人写的是lca,直接讨论时,感觉自己的智商收到了碾压... #include<cmat ...

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

  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. 编程字体Source Code Pro 免费下载

    对于程序员来说,好的字体应该满足的基本条件: 字母和数字易于分辨,如: 英文字母o 和 阿拉伯数字 0 ,或者 英文字母 l 和 阿拉伯数字 1 ,两个单引号 '' 和双引号 ”. 字体等宽,保持对齐 ...

  2. 20190827 On Java8 第十四章 流式编程

    第十四章 流式编程 流的一个核心好处是,它使得程序更加短小并且更易理解.当 Lambda 表达式和方法引用(method references)和流一起使用的时候会让人感觉自成一体.流使得 Java ...

  3. Java-集合第三篇List集合

    1.List集合 有序可重复集合,集合中的每个元素都有其对应的顺序索引. 2.List相对于Collection额外提供的方法: 1>void add(int index,Object elem ...

  4. 极*Java速成教程 - (3)

    Java语言基础 访问权限控制 Java是一个面向对象的语言,当你不是它所设计的要面向的对象时,它就不会给你看你不该看到的东西,也就是"访问权限控制". 亲疏有别,才能权限控制 包 ...

  5. [Codeforces 1205B]Shortest Cycle(最小环)

    [Codeforces 1205B]Shortest Cycle(最小环) 题面 给出n个正整数\(a_i\),若\(a_i \& a_j \neq 0\),则连边\((i,j)\)(注意i- ...

  6. [集合]List

    List 存取有序,有索引,可以重复 ArrayList去除集合中字符串的重复值(字符串的内容相同) public static void main(String[] args) { ArrayLis ...

  7. wamp 环境的配置

    安装完wamp之后,基本不用配置什么环境,只要将mysql添加到你的环境变量中去即可.

  8. (前篇:NIO系列 推荐阅读) Java NIO 底层原理

    出处: Java NIO 底层原理 目录 1.1. Java IO读写原理 1.1.1. 内核缓冲与进程缓冲区 1.1.2. java IO读写的底层流程 1.2. 四种主要的IO模型 1.3. 同步 ...

  9. 11-jQuery简介和选择器

    # jQuery > jQuery是一个是免费.开源的javascript库, 也是目前使用最广泛的javascript函数库.>> jQuery极大的方便你完成web前段的相关操作 ...

  10. HTML导航框架实现

    导航栏界面(html_contents.html) <!DOCTYPE html> <html> <head> <meta charset=” utf-8” ...