CF519E.A and B and Lecture Rooms

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

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

A 和 B 正在为编程竞赛做准备。

A 和 B 所在的大学由若干房间通过走廊连接而成。整个大学共有 $ n $ 个房间,由 $ n-1 $ 条走廊连接,使得任意两个房间之间均可通过走廊相互到达(即该结构构成一棵树)。房间编号为 $ 1 $ 到 $ n $。

每天,A 和 B 都会在大学中的某些房间内各自举办一场编程竞赛;每场竞赛结束后,他们都会在同一个房间会合并讨论题目。A 和 B 希望:该讨论房间到 A 的竞赛房间的距离,等于该讨论房间到 B 的竞赛房间的距离。此处,两房间之间的距离定义为连接它们的最短路径上的边数。

由于他们每天都会更换竞赛房间,他们请你帮忙计算:在接下来的 $ m $ 天中,每一天分别有多少个可能的房间可作为讨论房间。

输入格式

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.

第一行包含一个整数 nn(1≤n≤1051 \leq n \leq 10^5)—— 表示大学中房间的数量。

接下来的 n−1n-1 行描述走廊。其中第 ii 行(1≤i≤n−11 \leq i \leq n-1)包含两个整数 aia_i 和 bib_i(1≤ai,bi≤n1 \leq a_i, b_i \leq n),表示第 ii 条走廊连接房间 aia_i 和 bib_i。

下一行包含一个整数 mm(1≤m≤1051 \leq m \leq 10^5)—— 表示查询的数量。

接下来的 mm 行描述这些查询。其中第 jj 行(1≤j≤m1 \leq j \leq m)包含两个整数 xjx_j 和 yjy_j(1≤xj,yj≤n1 \leq x_j, y_j \leq n),表示在第 jj 天,A 将在房间 xjx_j 参加比赛,B 将在房间 yjy_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.

在第 ii 行(1 ≤ i ≤ m1 ≤ i ≤ m)输出第 ii 天 A 和 B 举行比赛的房间之间距离相等的房间数量。

输入输出样例

  • 输入#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.

在第一个样例中,只有一个房间与编号为 2 和 3 的房间距离相等——即编号为 1 的房间。

输入解题思路,AI测评打分。不知道怎么写?

首页