Tree and Query 15
Time limit5sMemory limit1024 MB
For each query, count the tree vertices within distance r_i of at least one of the k given vertices v_i.
- Level
Hard9 of 10
- Topics
- Tree, Divide and conquer, BFS
- Solved
- No attempts yet
Problem
There is a tree with vertices. The vertices are numbered from 1 to . Every edge has weight 1.
Write a program that handles the queries below.
k v1 r1 v2 r2 ... vk rk: A vertex satisfies condition if its distance to is at most . Print the number of vertices in the tree that satisfy at least one of the conditions in the query.
Input
The first line contains an integer (). The next lines each contain two vertices and connected by an edge ().
The next line contains an integer (). Each query does not fit on one line. Each query is split across lines, as shown in the sample input. Constraints: , , .
The sum of all values in the queries does not exceed 300000.
Output
For each query, print the result on its own line.