【Link】:http://codeforces.com/contest/832/problem/D

【Description】



给你一棵树;

然后给你3个点

让你把这3个点和点s,t,f对应;

然后s先从s走到f;

之后t再从t走到f;

求这两条路径的公共路径的长度;

【Solution】



答案为

dis(s,f)+dis(t,f)−dis(s,t)2

树上最短路径做一下就好;

LCA!



【NumberOf WA】



0



【Reviw】



想得太慢了



【Code】

  1. #include <cstdio>
  2. #include <vector>
  3. #include <iostream>
  4. #include <algorithm>
  5. using namespace std;
  6. const int MAXN = 100000+5;
  7. const int MAX = 17;
  8. vector <int> son[MAXN],w[MAXN];
  9. int n,p[MAXN][MAX+5],dep[MAXN],pre[MAX+5],m,t[3];
  10. long long dis[MAXN];
  11. void input(int &r)
  12. {
  13. char t = getchar();
  14. while (!isdigit(t)) t = getchar();
  15. r = 0;
  16. while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
  17. }
  18. void dfs(int x,int f)
  19. {
  20. dep[x] = dep[f] + 1;
  21. p[x][0] = f;
  22. for (int i = 1; i <= MAX; i++)
  23. p[x][i] = p[p[x][i - 1]][i - 1];
  24. int len = son[x].size();
  25. for (int i = 0; i <= len - 1; i++)
  26. {
  27. int y = son[x][i];
  28. if (y != f)
  29. {
  30. dis[y] = dis[x] + w[x][i];
  31. dfs(y, x);
  32. }
  33. }
  34. }
  35. int midis(int t0,int t1){
  36. int pret0,pret1;
  37. pret0 = t0; pret1 = t1;
  38. if (dep[t0] > dep[t1])
  39. swap(t0, t1);
  40. for (int i = MAX; i >= 0; i--)
  41. if (dep[t0] <= dep[t1] - pre[i])
  42. t1 = p[t1][i];
  43. if (t1 == t0)
  44. {
  45. return dis[pret0]+dis[pret1]-2*dis[t0];
  46. }
  47. for (int i = MAX; i >= 0; i--)
  48. {
  49. if (p[t0][i] == p[t1][i])
  50. continue;
  51. t0 = p[t0][i], t1 = p[t1][i];
  52. }
  53. return dis[pret0]+dis[pret1]-2*dis[p[t0][0]];
  54. }
  55. int get_ans(){
  56. int temp1 = 0;
  57. for (int S = 0;S <= 2;S++)
  58. for (int T = 0;T <= 2;T++)
  59. if (S!=T)
  60. for (int F = 0;F <= 2;F++)
  61. if (S!=F && T!=F){
  62. int temp = midis(t[S],t[F]);
  63. temp+=midis(t[T],t[F]);
  64. temp-=midis(t[S],t[T]);
  65. temp/=2;
  66. temp1 = max(temp1,temp);
  67. }
  68. return temp1;
  69. }
  70. int main()
  71. {
  72. //freopen("F:\\rush.txt", "r", stdin);
  73. pre[0] = 1;
  74. for (int i = 1; i <= MAX; i++)
  75. pre[i] = pre[i - 1] << 1;
  76. input(n); input(m);
  77. for (int i = 2; i <= n; i++)
  78. {
  79. int x;
  80. input(x);
  81. son[x].push_back(i);
  82. w[x].push_back(1);
  83. }
  84. dis[1] = 0;
  85. dfs(1, 0);
  86. for (int i = 1; i <= m; i++)
  87. {
  88. int t0, t1,t2,pret0,pret1;
  89. for (int j = 0;j <= 2;j++)
  90. input(t[j]);
  91. printf("%d\n",get_ans()+1);
  92. }
  93. return 0;
  94. }

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

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

  2. 【Codeforces Round #425 (Div. 2) B】Petya and Exam

    [Link]:http://codeforces.com/contest/832/problem/B [Description] *能代替一个字符串(由坏字母组成); ?能代替单个字符(由好字母组成) ...

  3. 【Codeforces Round #425 (Div. 2) A】Sasha and Sticks

    [Link]: [Description] [Solution] 傻逼题; 获取n/k; 对n/k的奇偶性讨论一下就好 [NumberOf WA] 0 [Reviw] [Code] #include ...

  4. 【Codeforces Round #432 (Div. 1) B】Arpa and a list of numbers

    [链接]h在这里写链接 [题意] 定义bad list是一个非空的.最大公约数为1的序列.给定一个序列,有两种操作:花费x将一个元素删除.花费y将一个元素加1,问你将这个序列变为good list所需 ...

  5. 【Codeforces Round #420 (Div. 2) C】Okabe and Boxes

    [题目链接]:http://codeforces.com/contest/821/problem/C [题意] 给你2*n个操作; 包括把1..n中的某一个数压入栈顶,以及把栈顶元素弹出; 保证压入和 ...

  6. 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees

    [题目链接]:http://codeforces.com/contest/821/problem/B [题意] 当(x,y)这个坐标中,x和y都为整数的时候; 这个坐标上会有x+y根香蕉; 然后给你一 ...

  7. 【Codeforces Round #420 (Div. 2) A】Okabe and Future Gadget Laboratory

    [题目链接]:http://codeforces.com/contest/821/problem/A [题意] 给你一个n*n的数组; 然后问你,是不是每个位置(x,y); 都能找到一个同一行的元素q ...

  8. 【Codeforces Round #423 (Div. 2) C】String Reconstruction

    [Link]:http://codeforces.com/contest/828/problem/C [Description] 让你猜一个字符串原来是什么; 你知道这个字符串的n个子串; 且知道第i ...

  9. 【Codeforces Round #423 (Div. 2) B】Black Square

    [Link]:http://codeforces.com/contest/828/problem/B [Description] 给你一个n*m的格子; 里面包含B和W两种颜色的格子; 让你在这个格子 ...

随机推荐

  1. [ACM] hdu 4248 A Famous Stone Collector (DP+组合)

    A Famous Stone Collector Problem Description Mr. B loves to play with colorful stones. There are n c ...

  2. Node.js 博客实例(十)pv统计和留言统计

    原教程 https://github.com/nswbmw/N-blog/wiki/_pages的第十章,因为版本号等的原因,在原教程基础上稍加修改就可以实现. post.js中将var post={ ...

  3. android:为TextView加入样式——下划线,颜色,设置链接样式及前背景色

    实现下划线及颜色设置: public class AtActivity extends Activity { LinearLayout ll;     /** Called when the acti ...

  4. JDK的安装及配置环境变量

    开发java程序的必备工具:JDK,全名是Java Development Kit, 是Java语言的软件开发工具包. 第一步:下载安装包 从Oracle官网可以选择自己所需的版本下载,(附Oracl ...

  5. ASMlib操作系统包安装与配置asm disk磁盘

    1.加入6块硬盘,每块100g.不管是热加还是冷加.不管是加硬盘,用san存储划lun,或者再加上多路径,都是能够这么做的. 在操作系统层,能识别这种lun.以下的sdb就是一个刚划分的300g的lu ...

  6. POJ 3187 全排列+杨辉三角(组合数)

    思路: next_permutation()加个递推组合数随便搞搞就A了- //By SiriusRen #include <cstdio> #include <algorithm& ...

  7. Android框架-Volley(三)

    经过前面两篇文章的学习,我们已经掌握了Volley各种Request的使用方法,包括StringRequest.JsonRequest.ImageRequest等.其中StringRequest用于请 ...

  8. Surfaces

    For Developers‎ > ‎Design Documents‎ > ‎Chromium Graphics // Chrome GPU‎ > ‎ Surfaces Goals ...

  9. ZOJ-3261 Connections in Galaxy War 并查集 离线操作

    题目链接:https://cn.vjudge.net/problem/ZOJ-3261 题意 有n个星星,之间有m条边 现一边询问与x星连通的最大星的编号,一边拆开一些边 思路 一开始是真不会,甚至想 ...

  10. CF718C Sasha and Array(线段树维护矩阵)

    题解 (不会矩阵加速的先去学矩阵加速) 反正我想不到线段树维护矩阵.我太菜了. 我们在线段树上维护一个区间的斐波那契的列矩阵的和. 然后询问时提取每个符合题意列矩阵的答案项(不是列矩阵存了两项吗,一个 ...