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.

Examples
input
  1. 3 2
    1 1
    1 2 3
    2 3 3
output
  1. 2
    3
input
  1. 4 1
    1 2 3
    1 2 3
output
  1. 2
Note

In the first example on the first day if s = 1, f = 2, t = 3, Misha would go on the route 1  2, and Grisha would go on the route 3  1  2. He would see the text at the stations 1 and 2. On the second day, if s = 3, f = 2, t = 3, both boys would go on the route 3  1  2. Grisha would see the text at 3 stations.

In the second examle if s = 1, f = 3, t = 2, Misha would go on the route 1  2  3, and Grisha would go on the route 2  3 and would see the text at both stations.

【题意】给你一棵树,然后q次询问,每次给出三个点a,b,c,要求从其中两个点走到第三个点,使得两种路径中共同经过的点最多,最多是多少?

【分析】枚举终点,然后判一下就行了。

  1. #include <bits/stdc++.h>
  2. #define inf 0x3f3f3f3f
  3. #define met(a,b) memset(a,b,sizeof a)
  4. #define pb push_back
  5. #define mp make_pair
  6. #define inf 0x3f3f3f3f
  7. using namespace std;
  8. typedef long long ll;
  9. const int N = 2e5+;
  10. const int M = ;
  11. const int mod = 1e9+;
  12. const double pi= acos(-1.0);
  13. typedef pair<int,int>pii;
  14. int n,m,ans;
  15. int dep[N],fa[N][];
  16. vector<int>edg[N];
  17. void dfs(int u,int f){
  18. fa[u][]=f;
  19. for(int i=;i<;i++){
  20. fa[u][i]=fa[fa[u][i-]][i-];
  21. }
  22. for(int v : edg[u]){
  23. if(v==f)continue;
  24. dep[v]=dep[u]+;
  25. dfs(v,u);
  26. }
  27. }
  28. int LCA(int u,int v){
  29. int U=u,V=v;
  30. if(dep[u]<dep[v])swap(u,v);
  31. for(int i=;i>=;i--){
  32. if(dep[fa[u][i]]>=dep[v]){
  33. u=fa[u][i];
  34. }
  35. }
  36. if(u==v)return (u);
  37. for(int i=;i>=;i--){
  38. if(fa[u][i]!=fa[v][i]){
  39. u=fa[u][i];v=fa[v][i];
  40. }
  41. }
  42. return (fa[u][]);
  43. }
  44. void solve(int u,int v,int t){
  45. int ut=LCA(u,t);
  46. int vt=LCA(v,t);
  47. if(dep[ut]<dep[t]&&dep[vt]<dep[t]){
  48. if(ut==vt){
  49. int uv=LCA(u,v);
  50. ans=max(ans,dep[t]-max(dep[ut],dep[vt])++dep[uv]-dep[ut]);
  51. }
  52. else ans=max(ans,dep[t]-max(dep[ut],dep[vt])+);
  53. }
  54. else if(ut==t&&vt==t){
  55. int uv=LCA(u,v);
  56. if(uv!=t)ans=max(ans,dep[uv]-dep[t]+);
  57. else ans=max(ans,);
  58. }
  59. else ans=max(ans,);
  60. }
  61. int main(){
  62. scanf("%d%d",&n,&m);
  63. for (int i=,v;i<=n;i++){
  64. scanf("%d",&v);
  65. edg[i].pb(v);
  66. edg[v].pb(i);
  67. }
  68. dep[]=;
  69. dfs(,);
  70. while(m--){
  71. int a,b,c;
  72. ans=;
  73. scanf("%d%d%d",&a,&b,&c);
  74. solve(a,b,c);
  75. solve(a,c,b);
  76. solve(b,c,a);
  77. printf("%d\n",ans);
  78. }
  79. return ;
  80. }

Codeforces Round #425 (Div. 2) Misha, Grisha and Underground(LCA)的更多相关文章

  1. Codeforces Round #184 (Div. 2) E. Playing with String(博弈)

    题目大意 两个人轮流在一个字符串上删掉一个字符,没有字符可删的人输掉游戏 删字符的规则如下: 1. 每次从一个字符串中选取一个字符,它是一个长度至少为 3 的奇回文串的中心 2. 删掉该字符,同时,他 ...

  2. Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)

    Problem  Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 1000 mSec Problem Descripti ...

  3. Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(分形)

    E. Dasha and Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. Codeforces Round #267 (Div. 2) C. George and Job(DP)补题

    Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...

  5. Codeforces Round #394 (Div. 2) C.Dasha and Password(暴力)

    http://codeforces.com/contest/761/problem/C 题意:给出n个串,每个串的初始光标都位于0(列)处,怎样移动光标能够在凑出密码(每个串的光标位置表示一个密码的字 ...

  6. Codeforces Round #107 (Div. 1) B. Quantity of Strings(推算)

    http://codeforces.com/problemset/problem/150/B 题意: 给出n,m,k,n表示字符串的长度为n,m表示字符种类个数,k表示每k个数都必须是回文串,求满足要 ...

  7. Codeforces Round #332 (Div. 2) D. Spongebob and Squares(枚举)

    http://codeforces.com/problemset/problem/599/D 题意:给出一个数x,问你有多少个n*m的网格中有x个正方形,输出n和m的值. 思路: 易得公式为:$\su ...

  8. Codeforces Round #419 (Div. 2) A. Karen and Morning(模拟)

    http://codeforces.com/contest/816/problem/A 题意: 给出一个时间,问最少过多少时间后是回文串. 思路: 模拟,先把小时的逆串计算出来: ① 如果逆串=分钟, ...

  9. Codeforces Round #390 (Div. 2) C. Vladik and chat(dp)

    http://codeforces.com/contest/754/problem/C C. Vladik and chat time limit per test 2 seconds memory ...

随机推荐

  1. Jmeter-6-创建数据库测试计划

    1. 将mysql 的jdbc的jar包放到Jmeter lib的目录下. 2. 创建线程组. 3. 创建JDBC Connection Configuration, 提供详细的数据库配置信息. 4. ...

  2. [uva11174]村民排队 递推+组合数+线性求逆元

    n(n<=40000)个村民排成一列,每个人不能排在自己父亲的前面,有些人的父亲不一定在.问有多少种方案. 父子关系组成一个森林,加一个虚拟根rt,转化成一棵树. 假设f[i]表示以i为根的子树 ...

  3. [uva11997]k个最小和

    一个k*k的矩阵,每行选取一个数相加则得到一个和,求最小的前k个和. k<=750 已知前m行最小的前k个和d[1]…d[k],则前m+1行最小的前k个和都必定是d[i](i<=k)+a[ ...

  4. 【BZOJ】1754: [Usaco2005 qua]Bull Math

    [算法]高精度乘法 #include<cstdio> #include<algorithm> #include<cstring> using namespace s ...

  5. Exponial (欧拉定理+指数循环定理+欧拉函数+快速幂)

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2021 Description Everybody loves big numbers ...

  6. 头像截取 图片上传 js插件

    先看一下整体效果 页面html <div class="row"> <div class="tabs-container"> <u ...

  7. js_实现给未来元素添加事件。

    未来元素:不是一个页面上的元素,是通过js或者通过后台直接渲染在页面上的元素,也就是说这些元素不是直接写在document中的. 1.对于未来元素,我们想直接用js或者jq操作它们是不起作用的. $( ...

  8. HTTP和HTTPS详解。

    一,HTTP和HTTPS基本概念 深入学习某个东西时,我们先来从维基百科上看看它俩的概念. HTTP:超文本传输协议(英文:HyperText Transfer Protocol,缩写:HTTP)是一 ...

  9. VMWare虚拟机NAT模式静态IP联网配置

    1.网络连接    vmnet8右键属性ipv4,设置ip为192.168.10.100.如下图: 2.设置虚拟机的网络适配器采用NAT模式 3.vmware工具栏的编辑->虚拟网络编辑器   ...

  10. perl中的lock

    #!/usr/bin/env perl -w use strict; use threads; use threads::shared; ; print "count的起始值为:$count ...