Description
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 - 1 corridors 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 a i and b i (1 ≤ a i, b i ≤ n), showing that the i-th corridor connects rooms a i and b i.
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 x j and y j (1 ≤ x j, y j ≤ n) that means that on the j-th day A will write the contest in the room x j, B will write in the room y j.
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.
Examples
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.
Main idea & solution
给你一棵树,有m次询问,每次询问在树上有多少个点离
x
i
,
y
i
x_i , y_i
xi,yi的距离相同。
我们发现
x
i
,
y
i
x_i, y_i
xi,yi之间的链
{
x
i
,
y
i
}
\left\{x_i,y_i\right\}
{xi,yi}上,中点是关键的点,我们找到中点后就很容易考虑答案了
这里用倍增来找中点
Hint
询问中可能会出现两个相同的点
Code
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
struct Edge{
int v,next;
}e[maxn<<1];
int ecnt,head[maxn];
inline void add(int u,int v){
e[++ecnt] = (Edge){v,head[u]}, head[u] = ecnt;
e[++ecnt] = (Edge){u,head[v]}, head[v] = ecnt;
}
int fa[maxn][32],dep[maxn], siz[maxn];
void dfs(int u,int f){
siz[u] = 1;
fa[u][0] = f;dep[u] = dep[fa[u][0]] + 1;
for(int i = 1;i < 31;++i){
fa[u][i] = fa[fa[u][i-1]][i-1];
}
for(int i = head[u];i;i = e[i].next){
int v = e[i].v;if(v == f) continue;
dfs(v,u);
siz[u] += siz[v];
}
}
int lca(int u,int v){
if(dep[u] > dep[v]) swap(u,v);
int tmp = dep[v] - dep[u];
for(int j = 0;tmp; ++j, tmp >>=1)
if(tmp & 1) v = fa[v][j];
if(u == v) return u;
for(int j = 30;j >= 0 && u != v;--j){
if(fa[u][j] != fa[v][j]){
u = fa[u][j];
v = fa[v][j];
}
}
return fa[u][0];
}
int solve(int u,int v,int n){
int k = lca(u,v), d = dep[u] + dep[v] - dep[k] * 2;
if(d % 2 == 1) return 0;
if(d == 0) return n;
if(dep[u] - dep[k] == dep[v] - dep[k]){
for(int j = 30;j >= 0 && u != v;--j){
if(fa[u][j] != fa[v][j]){
u = fa[u][j],v = fa[v][j];
}
}
return n - siz[u] - siz[v];
}
if(dep[u] - dep[k] < dep[v] - dep[k]) swap(u,v);
int tmp = d / 2 - 1;
int mid = u;
for(int j = 0;tmp;++j, tmp >>= 1)
if(tmp&1) mid = fa[mid][j];
return siz[fa[mid][0]] - siz[mid];
}
int main(){
int n;scanf("%d",&n);
for(int i = 1,u,v;i < n;++i){
scanf("%d%d",&u,&v);add(u,v);
}
dfs(1,1);
int q;scanf("%d",&q);
while(q--){
int u,v;scanf("%d%d",&u,&v);
int res = solve(u,v,n);
printf("%d\n", res);
}
return 0;
}