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. lvs三种负载均衡模式

    lvs模式 lvs nat模式 地址转换 nat模式 地址转发 ,数据全部集中在lvs处理,lvs压力大 lvs ip-tun模式 ip隧道 与nat模式差不多,差别有了隧道封装,轮询调度给后端服务器 ...

  2. [Usaco2005 mar]Yogurt factory 奶酪工厂

    接下来的N(1≤N10000)星期中,奶酪工厂在第i个星期要花C_i分来生产一个单位的奶酪.约克奶酪工厂拥有一个无限大的仓库,每个星期生产的多余的奶酪都会放在这里.而且每个星期存放一个单位的奶酪要花费 ...

  3. ementUi rules表单验证 --》Wangqi

    ElementUi rules表单验证   ElementUi 表单验证 工作中常用到的JS验证 可以在pattern中书写正则,并且配合elementUI进行表单验证. pattern 属性规定用于 ...

  4. Maven 标准项目结构

    项目结构 src main java         源文件 resources    资源文件 filters   资源过滤文件 config   配置文件 scripts   脚本文件 webap ...

  5. java类从加载、连接到初始化过程

    类加载器 在了解Java的机制之前,需要先了解类在JVM(Java虚拟机)中是如何加载的,这对后面理解java其它机制将有重要作用. 每个类编译后产生一个Class对象,存储在.class文件中,JV ...

  6. java基础常问面试题

    1.面向对象和面向过程的区别 面向过程 :面向过程性能比面向对象高. 因为类调用时需要实例化,开销比较大,比较消耗资源,所以当性能是最重要的考量因素的时候,比如单片机.嵌入式开发.Linux/Unix ...

  7. webpack搭建vue项目开发环境【文档向学习】

    为何有这篇文章 各个社区已经有无数篇帖子介绍如何使用webpack搭建前端项目,但无论是出于学习webpack的目的还是为了解决工作实际需要都面临着一个现实问题,那就是版本更新.别人的帖子可能刚写好版 ...

  8. C#设计模式:命令模式(Command Pattern)

    一,什么是命令模式(Command Pattern)? 命令模式:将请求封装成命令对象,请求的具体执行由命令接收者执行: 二,如下代码 using System; using System.Colle ...

  9. 如何同步发送put或者delete请求

    1.必须把前端发送方式改为post . 2.在web.xml中配置一个filter:HiddenHttpMethodFilter过滤器 3.必须携带一个键值对,key=_method,  value= ...

  10. 【转】一文搞懂C语言回调函数

    转:https://segmentfault.com/a/1190000008293902?utm_source=tag-newest 什么是回调函数 我们先来看看百度百科是如何定义回调函数的: 回调 ...