The Unstable Root

5

1 votes
Loops, Data Structures, Centroid decomposition, Tree, Medium-Hard
Problem

You are given a tree comprising n nodes numbered from 1 to n. You have to answer q queries each consisting of three numbers – rd1 and d2

For each query, print the number of nodes whose depth is between d1 and d2 (inclusive) if the tree is rooted at node r. The depth of root node itself is 0, the depth of all its children is 1 and so on.

Input format

First line: n (the number of nodes) and q (the number of queries)

Next n1 lines: ui and videnoting an edge between the nodes ui and vi.The next q lines consist of three integers rd1 and d2 .

Output format

For each query, print the answer in a new line.

input Constraints

1n1000001q1500000d1d2n1rn

 

Sample Input
5 2
1 2
1 3
2 4
4 5
1 0 2
2 0 1
Sample Output
4
3
Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

For node 1, there are 4 nodes whose depth is between 0 and 2 (1,2,3 and 4). For node 2 there are 3 nodes whose depth is between 0 and 1 (1,2 and 4).

Editor Image

?