测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

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

输入格式

The first line contains integer $n$ ( $1<=n<=10^{5}$ ) — 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<=10^{5}$ ) — 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}$ .

输出格式

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.

输入输出样例

输入 #1
4
1 2
1 3
2 4
1
2 3
输出 #1
1
输入 #2
4
1 2
2 3
2 4
2
1 2
1 3
输出 #2
0
2

说明/提示

in the first sample there is only one room at the same distance from rooms number 2 and 3 — room number 1.
上一题 去做题 下一题