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 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.
这场cf太水, 就写下这题的题解吧
给定一棵树,给定m次询问,每次给2个点,问有多少个点到这两个点的距离是一样的
dfs先预处理出以某一个节点为根的子树里节点的数目,num[u]
然后在线lca
分几种情况就行了
/************************************************************************* > File Name: cf-294-e.cpp > Author: ALex > Mail: zchao1995@gmail.com > Created Time: 2015年02月28日 星期六 22时13分38秒 ************************************************************************/
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
const int N = 101010;
int num[N];
int head[N];
int tot;
int deep[N];
int p[N][30];
struct node
{
int to;
int next;
}edge[N << 1];
void addedge (int from, int to)
{
edge[tot].to = to;
edge[tot].next = head[from];
head[from] = tot++;
}
void dfs (int u, int fa)
{
for (int i = head[u]; ~i; i = edge[i].next)
{
int v = edge[i].to;
if (v == fa)
{
continue;
}
deep[v] = deep[u] + 1;
p[v][0] = u;
dfs (v, u);
}
}
int lca (int a, int b)
{
if (deep[a] < deep[b])
{
swap (a, b);
}
int d = deep[a] - deep[b];
for (int i = 0; i < 30; ++i)
{
if (d & (1 << i))
{
a = p[a][i];
}
}
if (a == b)
{
return a;
}
for (int i = 29; i >= 0; --i)
{
if (p[a][i] != p[b][i])
{
a = p[a][i];
b = p[b][i];
}
}
return p[a][0];
}
int dfs2 (int u, int fa)
{
num[u] = 1;
for (int i = head[u]; ~i; i = edge[i].next)
{
int v = edge[i].to;
if (v == fa)
{
continue;
}
num[u] += dfs2 (v, u);
}
return num[u];
}
int main ()
{
int n;
while (~scanf("%d", &n))
{
int u, v;
memset (head, -1, sizeof(head));
tot = 0;
for (int i = 0; i < n - 1; ++i)
{
scanf("%d%d", &u, &v);
addedge (u, v);
addedge (v, u);
}
dfs(1, -1);
memset (num, 0, sizeof(num));
deep[1] = 0;
dfs2 (1, -1);
for (int j = 1; j < 30; ++j)
{
for (int i = 1; i <= n; ++i)
{
p[i][j] = p[p[i][j - 1]][j - 1];
}
}
int m;
scanf("%d", &m);
while (m--)
{
scanf("%d%d", &u, &v);
if (u == v)
{
printf("%d\n", n);
continue;
}
int LCA = lca (u, v);
int d1 = deep[u] - deep[LCA];
int d2 = deep[v] - deep[LCA];
if (d1 != d2)
{
if (abs(d1 - d2) & 1)
{
printf("0\n");
}
else
{
if (deep[u] < deep[v])
{
swap (u, v);
}
int dist = (d1 + d2) / 2;
int uu = u;
for (int k = 0; k < 30; ++k)
{
if (dist & (1 << k))
{
uu = p[uu][k];
}
}
--dist;
int vv = u;
for (int k = 0; k < 30; ++k)
{
if (dist & (1 << k))
{
vv = p[vv][k];
}
}
printf("%d\n", num[uu] - num[vv]);
}
}
else
{
int uu = u;
int dist = d1 - 1;
for (int k = 0; k < 30; ++k)
{
if (dist & (1 << k))
{
uu = p[uu][k];
}
}
int vv = v;
for (int k = 0; k < 30; ++k)
{
if (dist & (1 << k))
{
vv = p[vv][k];
}
}
printf("%d\n", n - num[vv] - num[uu]);
}
}
}
return 0;
}