#P6626. [省选联考 2020 B 卷] 消息传递

[省选联考 2020 B 卷] 消息传递

Description

Given a tree-shaped social network with nn people (numbered from 11 to nn). If a person receives a message on some day, then on the next day they will pass the message to all people who have a direct social relationship with them.

Now there are mm queries. In each query, assume that person xx receives a message on day 00. Please compute how many people newly receive this message on day kk (that is, people who have received this message before day kk are not counted). Different queries do not affect each other.

Input Format

This problem contains multiple sets of testdata.

The first line contains an integer TT, the number of test cases.

For each test case:

The first line contains two integers n,mn, m, representing the number of people in the tree-shaped social network and the number of queries.

The next n1n - 1 lines each contain two integers a,ba, b, indicating that there is a direct social relationship between person aa and person bb. It is guaranteed that the input forms a tree-shaped social network.

The next mm lines each contain two integers x,kx, k, as described in the statement.

Output Format

For each test case, output mm lines, each containing one integer representing the answer to the query.

1
4 2
1 2
2 3
3 4
1 1
2 2
1
1

Hint

Sample Explanation

For the first query, the only person who newly receives the message on day 11 is person 22. For the second query, the people who newly receive the message on day 11 are persons 11 and 33, and the person who newly receives the message on day 22 is person 44.

Constraints and Notes

For test point 11: 1n,m101\le n, m\le 10.
For test point 22: 1n,m1001\le n, m\le 100.
For test point 33: 1n,m10001\le n, m\le 1000.
For test points 464\sim6: 1n,m105,k201\le n, m\le 10^5, k\le 20.
For test points 7107\sim10: 1n,m1051\le n, m\le 10^5.
For all test points: 1T5,1xn,0k<n1\le T\le 5, 1\le x\le n, 0\le k < n.

Translated by ChatGPT 5