转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

E. A and B and Lecture Rooms

A and B are preparing themselves for programming contests.

The University where A and B study is a set of rooms connected by corridors. Overall, the University has n rooms connected by n - 1corridors so that you can get from any room to any other one by moving along the corridors. The rooms are numbered from 1 to n.

Every day А and B write contests in some rooms of their university, and after each contest they gather together in the same room and discuss problems. A and B want the distance from the rooms where problems are discussed to the rooms where contests are written to be equal. The distance between two rooms is the number of edges on the shortest path between them.

As they write contests in new rooms every day, they asked you to help them find the number of possible rooms to discuss problems for each of the following m days.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of rooms in the University.

The next n - 1 lines describe the corridors. The i-th of these lines (1 ≤ i ≤ n - 1) contains two integers ai and bi (1 ≤ ai, bi ≤ n), showing that the i-th corridor connects rooms ai and bi.

The next line contains integer m (1 ≤ m ≤ 105) — the number of queries.

Next m lines describe the queries. The j-th of these lines (1 ≤ j ≤ m) contains two integers xj and yj (1 ≤ xj, yj ≤ n) that means that on the j-th day A will write the contest in the room xj, B will write in the room yj.

Output

In the i-th (1 ≤ i ≤ m) line print the number of rooms that are equidistant from the rooms where A and B write contest on the i-th day.

Sample test(s)
input
4
1 2
1 3
2 4
1
2 3
output
1
input
4
1 2
2 3
2 4
2
1 2
1 3
output
0
2
Note

in the first sample there is only one room at the same distance from rooms number 2 and 3 — room number 1.

题意:

给出一棵树,有m次查询,每次查询给出两个点,要求求出这棵树上所有到这两个点距离相等的点的数目

分析:

首先求出两个点的距离,若距离为奇数,则不存在满足要求的点,若距离为偶数,则去路径上的中点,求出所有以从中点连出的分支,同时减去与给出的两点在同一分支上的点的数目

考虑到后面要求深度较深的点向上爬距离为两点间距离一半路程,用倍增在前面处理过的话,这里可以用log n 得到,所以采取了倍增

 //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
#define MAXN 100010
vector<int>G[MAXN];
int root;
int size[MAXN];
int tot;
int pa[][MAXN];
int depth[MAXN];
void dfs(int v,int fa,int d){
pa[][v]=fa;
size[v]=;
depth[v]=d;
for(int i=;i<G[v].size();i++){
if(G[v][i]==fa)continue;
dfs(G[v][i],v,d+);
size[v]+=size[G[v][i]];
}
}
void init(){
dfs(,-,);
for(int k=;k+<;k++){
for(int v=;v<tot;v++){
if(pa[k][v]<)pa[k+][v]=-;
else pa[k+][v]=pa[k][pa[k][v]];
}
}
}
int lca(int u,int v){
if(depth[u]>depth[v])swap(u,v);
int dis=depth[v]-depth[u];
for(int k=;k<;k++){
if((dis>>k)&){
v=pa[k][v];
}
}
if(u==v)return u;
for(int k=;k>=;k--){
if(pa[k][u]!=pa[k][v]){
u=pa[k][u];
v=pa[k][v];
}
}
return pa[][u];
}
int main()
{
ios::sync_with_stdio(false);
int n;
cin>>n;
tot=n;
int u,v;
for(int i=;i<n-;i++){
cin>>u>>v;
u--;v--;
G[u].push_back(v);
G[v].push_back(u);
}
init();
int m;
cin>>m;
for(int i=;i<m;i++){
cin>>u>>v;
u--;v--;
int r=lca(u,v);
int dis=depth[u]+depth[v]-*depth[r];
if(depth[u]>depth[v])swap(u,v);
if(dis&){
cout<<<<endl;
}else{
dis/=;
int mid=v;
for(int k=;k>=;k--){
if((dis>>k)&){
mid=pa[k][mid];
}
}
int ans=;
if(mid==r){
int preu=u,prev=v;
int du=depth[u]-depth[r];
du--;
for(int k=;k>=;k--){
if((du>>k)&){
preu=pa[k][preu];
}
}
int dv=depth[v]-depth[r];
dv--;
for(int k=;k>=;k--){
if((dv>>k)&){
prev=pa[k][prev];
}
}
ans=tot-size[preu]-size[prev];
}else{
int prev=v,preu=u;
int dv=depth[v]-depth[mid];
dv--;
for(int k=;k>=;k--){
if((dv>>k)&){
prev=pa[k][prev];
}
}
ans=size[mid]-size[prev];
}
cout<<ans<<endl;
}
} return ;
}

codeforces 519E A and B and Lecture Rooms(LCA,倍增)的更多相关文章

  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

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

  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 519E]E. A and B and Lecture Rooms(树上倍增)

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

  6. A and B and Lecture Rooms(LCA)

    题目描述 A and B are preparing themselves for programming contests. The University where A and B study i ...

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

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

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

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

随机推荐

  1. CODEVS 3279 奶牛的健美操

    3279 奶牛健美操 USACO  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题目描述 Description Farmer John为了保持奶牛们的 ...

  2. VC编程命名方法

    1.

  3. angular.js学习

    1.第一个小例子 <!DOCTYPE html> <html> <body> <div ng-app=""> <p>在输 ...

  4. BasicExcel说明文档

    BasicExcel说明文档 BasicExcel原始链接:http://www.codeproject.com/Articles/13852/BasicExcel-A-Class-to-Read-a ...

  5. UITableView属性和方法

    1.初始化一个UITableView - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style struct CGRect { C ...

  6. 感知机(perceptron)

    二类分类的线性分类模型,属于判别模型,利用梯度下降法对损失函数进行极小化求得感知机模型分为原始形式和对偶形式,是神经网络和支持向量机的基础 由输入控件到输出控件的如下函数: f(x)=sign(W.X ...

  7. Oracle查看表空间及修改数据文件大小

    Oracle查看表空间及修改数据文件大小 第一步:查看所有表空间及表空间大小: select tablespace_name ,sum(bytes) / 1024 / 1024 as MB from ...

  8. HttpStatusCode 枚举

    .NET Framework 类库 HttpStatusCode 枚举 包含为 HTTP 定义的状态代码的值. 命名空间:System.Net程序集:System(在 system.dll 中)   ...

  9. Magento How To Display Product Custom Option On list.phtml

    Some time we need to display custom option of product on category list page to achive this task we o ...

  10. 【转】Java基本数据类型

    原文网址:http://blog.csdn.net/bingduanlbd/article/details/27790287 Java语言是静态类型的(statical typed),也就是说所有变量 ...