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. 比较oracle数据的表结构

    对比不同用户对象的异同,同时生成sql语句或直接提交到数据库,powerdesinger的比较实在是麻烦. pl/sql为我们提供了很好的工具 在pl/sql中的工具下“比较用户对象”,下即可实现:

  2. SpringMVC中的设计模式

    1.<跟我学SpringMVC> P10 2.<跟我学SpringMVC> P32

  3. JavaScript排序算法——归并排序

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. HTTP 传输内容的压缩

    一.HTTP压缩和内容编码的区别 HTTP压缩,在HTTP协议中,其实是内容编码的一种. 在http协议中,可以对内容(也就是body部分)进行编码, 可以采用gzip这样的编码. 从而达到压缩的目的 ...

  5. OBJ Loader Source Code

    https://github.com/ChrisJansson/ObjLoader http://www.codeproject.com/Articles/798054/SimpleScene-d-s ...

  6. wampserver 2.5安装pear win8.1

    集成环境的悲伤啊~ 本来看到pear想试试 结果发现根本没有go-pear.bat 自己的环境 都是 系统win 8.1 php 5.5.12 mysql  5.6.17 apache 2.4.9   ...

  7. python 包管理工具

    python 包管理工具 Python当前的包管理工具链是 easy_install/pip + distribute/setuptools + distutils,显得较为混乱. 而将来的工具链组合 ...

  8. ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting 【转】

    ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting FEBRUARY 27, 2012 14 COMMENTS WebG ...

  9. a标签的target指向iframe

    <html> <head> <meta charset="utf-8" /> </head> <body> <ta ...

  10. php开发总结

    ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=shared,mys ...