http://codeforces.com/contest/519/problem/E

题意:

给出一棵树和m次询问,每次询问给出两个点,求出到这两个点距离相等的点的个数。

思路:

lca...然后直接判就好了,挂dp标签的人是什么心态。。

  1. #include<cstdio>
  2. #include<cmath>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<iostream>
  6. int tot,go[],first[],next[];
  7. int son[],fa[][];
  8. int deep[];
  9. int n,bin[];
  10. int read(){
  11. int t=,f=;char ch=getchar();
  12. while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
  13. while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
  14. return t*f;
  15. }
  16. void insert(int x,int y){
  17. tot++;
  18. go[tot]=y;
  19. next[tot]=first[x];
  20. first[x]=tot;
  21. }
  22. void add(int x,int y){
  23. insert(x,y);insert(y,x);
  24. }
  25. void dfs(int x,int f){
  26. son[x]=;
  27. for (int i=;i<=;i++)
  28. fa[x][i]=fa[fa[x][i-]][i-];
  29. for (int i=first[x];i;i=next[i]){
  30. int pur=go[i];
  31. if (pur==f) continue;
  32. deep[pur]=deep[x]+;
  33. fa[pur][]=x;
  34. dfs(pur,x);
  35. son[x]+=son[pur];
  36. }
  37. }
  38. int lca(int x,int y){
  39. if (deep[x]<deep[y]) std::swap(x,y);
  40. int t=deep[x]-deep[y];
  41. for (int i=;i<=;i++)
  42. if (t&bin[i])
  43. x=fa[x][i];
  44. if (x==y) return x;
  45. for (int i=;i>=;i--)
  46. if (fa[x][i]!=fa[y][i])
  47. x=fa[x][i],y=fa[y][i];
  48. if (x==y) return x;
  49. else return fa[x][];
  50. }
  51. void up(int &x,int dep){
  52. int t=deep[x]-dep;
  53. for (int i=;i<=;i++)
  54. if (t&bin[i])
  55. x=fa[x][i];
  56. }
  57. void work(int x,int y){
  58. int t=lca(x,y);
  59. int len=deep[x]-deep[t]++deep[y]-deep[t]+-;
  60. if (len%==){
  61. printf("0\n");
  62. return;
  63. }
  64. if (x==y){
  65. printf("%d\n",n);
  66. return;
  67. }
  68. if (t==x||t==y){
  69. if (t==x) std::swap(x,y);
  70. int tt=x;
  71. up(tt,deep[tt]-len/);
  72. up(x,deep[tt]+);
  73. printf("%d\n",son[tt]-son[x]);
  74. }else{
  75. if (deep[x]<deep[y]) std::swap(x,y);
  76. if (deep[x]==deep[y]){
  77. up(x,deep[t]+);
  78. up(y,deep[t]+);
  79. printf("%d\n",n-son[x]-son[y]);
  80. }else{
  81. int tt=x;
  82. up(tt,deep[tt]-len/);
  83. up(x,deep[tt]+);
  84. printf("%d\n",son[tt]-son[x]);
  85. }
  86. }
  87. }
  88. int main(){
  89. n=read();bin[]=;for (int i=;i<=;i++) bin[i]=bin[i-]*;
  90. for (int i=;i<n;i++){
  91. int x=read(),y=read();
  92. add(x,y);
  93. }
  94. dfs(,);
  95. int T=read();
  96. while (T--){
  97. int x=read(),y=read();
  98. work(x,y);
  99. }
  100. return ;
  101. }

Codeforces 519E A and B and Lecture Rooms的更多相关文章

  1. codeforces 519E A and B and Lecture Rooms LCA倍增

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Prac ...

  2. codeforces 519E A and B and Lecture Rooms(LCA,倍增)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud E. A and B and Lecture Rooms A and B are ...

  3. CodeForces 519E A and B and Lecture Rooms(倍增)

    A and B are preparing themselves for programming contests. The University where A and B study is a s ...

  4. Codeforces 519E A and B and Lecture Rooms [倍增法LCA]

    题意: 给你一棵有n个节点的树,给你m次询问,查询给两个点,问树上有多少个点到这两个点的距离是相等的.树上所有边的边权是1. 思路: 很容易想到通过记录dep和找到lca来找到两个点之间的距离,然后分 ...

  5. Codeforces Round #294 (Div. 2) A and B and Lecture Rooms(LCA 倍增)

    A and B and Lecture Rooms time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  6. [CF Round #294 div2] E. A and B and Lecture Rooms 【树上倍增】

    题目链接:E. A and B and Lecture Rooms 题目大意 给定一颗节点数10^5的树,有10^5个询问,每次询问树上到xi, yi这两个点距离相等的点有多少个. 题目分析 若 x= ...

  7. [codeforces 519E]E. A and B and Lecture Rooms(树上倍增)

    题目:http://codeforces.com/problemset/problem/519/E 题意:给你一个n个点的树,有m个询问(x,y),对于每个询问回答树上有多少个点和x,y点的距离相等 ...

  8. CodeForces 519E 树形DP A and B and Lecture Rooms

    给出一棵树,有若干次询问,每次询问距两个点u, v距离相等的点的个数. 情况还挺多的,少侠不妨去看官方题解.^_^ #include <iostream> #include <cst ...

  9. Codeforces 519 E. A and B and Lecture Rooms

    Description 询问一个树上与两点距离相等的点的个数. Sol 倍增求LCA. 一棵树上距离两点相等,要么就只有两点的中点,要么就是与中点相连的所有点. 有些结论很容易证明,如果距离是偶数,那 ...

随机推荐

  1. CCF 送货 + 欧拉路模板

    #include <bits/stdc++.h> using namespace std; stack<int> st; vector<]; ][]; ],cp[]; i ...

  2. [饭后算法系列] 数组中"和非负"的最长子数组

    1. 问题 给定一列数字数组 a[n], 求这个数组中最长的 "和>=0" 的子数组. (注: "子数组"表示下标必须是连续的. 另一个概念"子 ...

  3. BeanstalkClient学习

    针对BeanstalkClient-1.4.6.jar 生产者 示例代码: package com.lky.test; import java.io.UnsupportedEncodingExcept ...

  4. 基于Hadoop集群的HBase集群的配置

    一  Hadoop集群部署 hadoop配置 二 Zookeeper集群部署 zookeeper配置 三  Hbase集群部署 1.配置hbase-env.sh HBASE_MANAGES_ZK:用来 ...

  5. uploadify不能正确显示中文的按钮文本的解决办法

    uploadify 目前不能正确显示中文的按钮文本. 我发现bug的原因是uploadify错误的使用了 js 的 escape 和 flash 的 unescape配对,而这2个是不兼容的.正确的转 ...

  6. [置顶] .net技术类面试、笔试题汇总3

    今天本人从成都回到了学校,深刻认识了自己存在很多不足,在这段期间会更加努力,争取早日找到一个好工作! 41.在ASP.NET中有Button控件myButton,要是单击控件时,导航到其他页面http ...

  7. 【剑指Offer学习】【面试题56:链表中环的入口结点】

    题目:一个链表中包括环.怎样找出环的入口结点? 解题思路 能够用两个指针来解决问题.先定义两个指针P1和P2指向链表的头结点.假设链表中环有n个结点,指针P1在链表上向前移动n步,然后两个指针以同样的 ...

  8. 复合命令A等效于$a

    时间:2014.06.28 地点:基地 ------------------------------------------------------------------------------- ...

  9. Creating Apps With Material Design —— Defining Shadows and Clipping Views

    View转载请注明 http://blog.csdn.net/eclipsexys 翻译自Developer Android,时间仓促,有翻译问题请留言指出,谢谢 定义阴影和裁减 材料设计引入了深度的 ...

  10. QLCDNumber设置背景色和显示数字颜色

          只看楼主 倒序阅读楼主  发表于: 2013-10-22          //LCD时间显示    QLCDNumber *m_pLcdTime = new QLCDNumber(thi ...