#C8862. Counting Nodes with Exactly k Ancestors
Counting Nodes with Exactly k Ancestors
Counting Nodes with Exactly k Ancestors
You are given a tree (a connected acyclic undirected graph) with n nodes. For each test case, you are required to answer q queries. In each query, you are given an integer k; your task is to determine the number of nodes in the tree that have exactly k ancestors (i.e., nodes at depth k when considering node 1 as the root).
Note that the number of ancestors of the root (node 1) is 0 since it does not have any ancestors.
Input and output should be processed through standard input (stdin) and standard output (stdout) respectively.
inputFormat
The input starts with an integer t (1 ≤ t ≤ 100) representing the number of test cases. Each test case is described as follows:
- The first line contains two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 105), denoting the number of nodes and the number of queries respectively.
- The next n - 1 lines describe the edges of the tree. Each of these lines contains two integers u and v (1 ≤ u, v ≤ n) representing an edge between nodes u and v.
- The following q lines each contain one integer k (0 ≤ k < n), representing a query.
outputFormat
For each test case, output q lines. Each line should contain the number of nodes with exactly k ancestors (i.e. nodes at depth k), in the same order as the queries appear in the input.
## sample1
5 3
1 2
1 3
2 4
2 5
0
1
2
1
2
2
</p>