CF519E.A and B and Lecture Rooms

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 nn rooms connected by n1n-1 corridors so that you can get from any room to any other one by moving along the corridors. The rooms are numbered from 11 to nn .

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

输入格式

The first line contains integer nn ( 1<=n<=1051<=n<=10^{5} ) — the number of rooms in the University.

The next n1n-1 lines describe the corridors. The ii -th of these lines ( 1<=i<=n11<=i<=n-1 ) contains two integers aia_{i} and bib_{i} ( 1<=ai,bi<=n1<=a_{i},b_{i}<=n ), showing that the ii -th corridor connects rooms aia_{i} and bib_{i} .

The next line contains integer mm ( 1<=m<=1051<=m<=10^{5} ) — the number of queries.

Next mm lines describe the queries. The jj -th of these lines ( 1<=j<=m1<=j<=m ) contains two integers xjx_{j} and yjy_{j} ( 1<=xj,yj<=n1<=x_{j},y_{j}<=n ) that means that on the jj -th day A will write the contest in the room xjx_{j} , B will write in the room yjy_{j} .

输出格式

In the ii -th ( 1<=i<=m1<=i<=m ) line print the number of rooms that are equidistant from the rooms where A and B write contest on the ii -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.

首页