Finding Interesting Stuff
Time Limit: 10 Seconds Memory Limit: 65536 KB
There is a tree with n vertices, which are numbered by integers from 1 to n, and there are q queries which we need to find an answer for. Each query can be described with two integers l and r. A vertex v is “interesting”, if and only if l ≤ v ≤ r and an edge (u, v) is “interesting”, if and only if both u and v are “interesting”. Find the number of connected components consist of all the interesting vertices and the interesting edges for each query.
Input
The first line of the input is an integer T < 8, indicating the number of test cases. For each test case, the first line contains two integers n and q (1 ≤ n, q ≤ 2*105), indicating the number of vertices and the number of queries. The following n-1 lines each contain two integers u and v (1 ≤ u, v ≤ n), indicating an edge connecting vertex u and v in the tree. The following q lines each contain two integers l and r (1 ≤ l ≤ r ≤ n), indicating a query. It's guaranteed that the given graph is a tree.
Output
For each query output one line containing one integer, indicating the answer. Hint: For the six queries in case 1, the connected components are listed as follows: [1], [2] [2, 3] [3, 4] [1], [2, 3] [2, 3, 4] [1, 2, 3, 4] For the two queries in case 2, the connected components are as follows: [1], [2] [2, 3]
Sample Input
2
4 6
1 4
4 3
3 2
1 2
2 3
3 4
1 3
2 4
1 4
3 2
1 3
2 3
1 2
2 3
Sample Output
2
1
1
2
1
1
2
1
Submit