#LCA0000059. Những phòng học cách đều (A and B and Lecture Rooms)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Những phòng học cách đều (A and B and Lecture Rooms)
A and B and Lecture Rooms
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
A university has rooms linked by undirected corridors forming a tree. Two students start in rooms and wish to meet in a room whose distance from equals its distance from . Distances count corridors traversed.
For every pair , count the rooms satisfying this equality. If , every room is valid.
Input
- The first line contains .
- The next lines describe edges .
- The following line contains .
- The last lines each contain rooms .
Output
Print one integer per query: the number of rooms equidistant from its two endpoints.
Subtasks
General constraints: ; .
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
5
1 2
1 3
3 4
3 5
4
1 1
2 4
4 5
1 4
Output
5
0
3
2
Explanation
For , all five rooms work. The path has odd length, giving no equidistant room. On path , rooms are equidistant from and , so the count is . For , rooms and are equally distant from endpoints, giving .