Description

询问一个树上与两点距离相等的点的个数.

Sol

倍增求LCA.

一棵树上距离两点相等,要么就只有两点的中点,要么就是与中点相连的所有点.

有些结论很容易证明,如果距离是偶数,那么他们没有中点,树上不存在距离两点相等的点.

如果中点恰好是两点LCA,那么答案就是\(n-size_x-size_y\) ,\(size_x\) 和 \(size_y\) 表示LCA的子节点中子树包含 \(u,v\) 的子节点的\(size\)

如果不是LCA,那么答案就是 \(size_{LCA}-size_x\) 是深度靠下的点.

PS:Loli的破机子跑的真tm慢.

Code

#include<cstdio>
#include<utility>
#include<vector>
#include<iostream>
using namespace std; #define mpr(a,b) make_pair(a,b)
#define debug(a) cout<<#a<<"="<<a<<" "
const int N = 100005;
const int M = 21; int n;
int f[N][M],d[N],sz[N],pow2[M];
vector<int> g[N]; inline int in(int x=0,char ch=getchar()){ while(ch>'9'|| ch<'0') ch=getchar();
while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x; }
void DFS(int u,int fa,int dep){
f[u][0]=fa,sz[u]=1,d[u]=dep;
for(int i=0,v;i<g[u].size();i++) if((v=g[u][i])!=fa){
DFS(v,u,dep+1);
sz[u]+=sz[v];
}
}
pair<int,int> LCA(int u,int v){
if(d[u]<d[v]) swap(u,v);
int l=d[u]-d[v],res=0;
for(int i=0;i<M;i++){
if(l&pow2[i]) res=res+pow2[i],u=f[u][i];
}
if(u==v) return mpr(res,u);
for(int i=M-1;~i;i--) if(f[u][i]!=f[v][i]) u=f[u][i],v=f[v][i],res+=pow2[i]*2;
return mpr(res+2,f[u][0]);
}
int Getp(int u,int d){
for(int i=0;i<M;i++) if(d & pow2[i]) u=f[u][i];
return u;
}
void solve(int u,int v){
if(d[u]<d[v]) swap(u,v);
pair<int,int> pr=LCA(u,v); // debug(pr.first),debug(pr.second)<<endl; if(u==v){ printf("%d\n",n);return; }
if(pr.first & 1){ printf("%d\n",0);return; } int mid=Getp(u,pr.first/2);
int uu=Getp(u,pr.first/2-1),vv=Getp(v,pr.first/2-1); // debug(mid),debug(uu),debug(vv)<<endl; if(mid == pr.second){
printf("%d\n",n-sz[uu]-sz[vv]);
}else{
printf("%d\n",sz[mid]-(f[uu][0]==mid ? sz[uu] : 0) - (f[vv][0]==mid ? sz[vv] : 0));
}
}
int main(){ n=in();
for(int i=1,u,v;i<n;i++) u=in(),v=in(),g[u].push_back(v),g[v].push_back(u);
pow2[0]=1;for(int i=1;i<M;i++) pow2[i]=pow2[i-1]<<1; DFS(1,0,1);
for(int j=1;j<M;j++) for(int i=1;i<=n;i++) f[i][j]=f[f[i][j-1]][j-1]; for(int m=in(),u,v;m--;) u=in(),v=in(),solve(u,v);
return 0;
}

  

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

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

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

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

  4. [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= ...

  5. Codeforces 519E A and B and Lecture Rooms

    http://codeforces.com/contest/519/problem/E 题意: 给出一棵树和m次询问,每次询问给出两个点,求出到这两个点距离相等的点的个数. 思路: lca...然后直 ...

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

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

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

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

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

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

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

随机推荐

  1. Jquery学习之基础篇二

    1.Jquery添加元素的方法 append() - 在被选元素的结尾插入内容 prepend() - 在被选元素的开头插入内容 after() - 在被选元素之后插入内容 before() - 在被 ...

  2. JavaWeb学习笔记——DOM4J

    下载的地址为:http://www.dom4j.org/dom4j-1.6.1/ import java.io.File; import java.io.FileOutputStream; impor ...

  3. Java排序算法——选择排序

    import java.util.Arrays; //================================================= // File Name : Select_S ...

  4. 《CSS3实战》读书笔记 第4章:样式继承

    节省你的时间--样式继承 ### 什么是继承? 后代元素可以继承先代元素的一些属性.有了它,可以省去分别定义样式的时间.继承了方向是由外而内的. ### 继承的局限性 应该注意到,有些属性是不适宜继承 ...

  5. yourphp基本语句

    实例化页面代码 1.时间代码:{$vo.createtime|toDate=###,'Y-m-d H:i:s'} 2.连接:{:U('Pro/arr')},{:URL()} 如:<form ac ...

  6. UIAlertController 标题文字大小 颜色

    NSString *title = [NSString stringWithFormat:]; NSString *msg = @"\n把红包分享给微信好友,金额随机,可用于购买雪票和雪卡& ...

  7. Web Pages - Efficient Paging Without The WebGrid

    Web Pages - Efficient Paging Without The WebGrid If you want to display your data over a number of p ...

  8. android自定义控件(2)-拖拽实现开关切换

    在这里,我们的主要工作就是在原有代码的基础上,增加一个重写的onTouchEvent方法,刚添加上来的时候是这个样子的: @Override public boolean onTouchEvent(M ...

  9. Makefile的学习笔记

    Makefile的学习笔记 标签: makefilewildcard扩展includeshellfile 2012-01-03 00:07 9586人阅读 评论(2) 收藏 举报  分类: Linux ...

  10. 深入理解Javascript面向对象编程

    深入理解Javascript面向对象编程 阅读目录 一:理解构造函数原型(prototype)机制 二:理解原型域链的概念 三:理解原型继承机制 四:理解使用类继承(继承的更好的方案) 五:建议使用封 ...