#P16905. [CCO 2026] Tree Traversals

[CCO 2026] Tree Traversals

Problem Description

Yevin Kang has a tree with NN vertices that are labelled with integers from 11 to NN. A tree is an undirected connected graph that does not contain a cycle.

Let KK be a positive integer. We define f(K)f(K) as follows.

For any two vertices 1u,vN1 \le u, v \le N, let d(u,v)d(u, v) denote the number of edges on the simple path connecting vertex uu and vertex vv. In particular, d(u,u)=0d(u, u) = 0 for all 1uN1 \le u \le N.

A permutation p1,,pNp_1, \ldots, p_N of 1,,N1, \ldots, N is good if all of the following conditions are satisfied.

  • d(pi1,pi)Kd(p_{i-1}, p_i) \le K for all i=2,,Ni = 2, \ldots, N.
  • d(1,pi)d(1,pj)d(1, p_i) \le d(1, p_j) for all pairs of integers (i,j)(i, j) with 1i<jN1 \le i < j \le N.

Then, f(K)f(K) is the number of good permutations.

Yevin thinks this problem is too easy, so he gives you QQ positive integers K1,,KQK_1, \ldots, K_Q. He asks you to print the values of f(K1),f(K2),,f(KQ)f(K_1), f(K_2), \ldots, f(K_Q), mod 109+710^9 + 7.

It may also be useful to note that “mod” corresponds to the %\% operator in most programming languages, indicating the remainder after division. For example, 5mod3=25 \bmod 3 = 2 and 17mod4=117 \bmod 4 = 1.

Input Format

Each test has multiple test cases.

The first line of the test contains one integer TT (1T5×1051 \le T \le 5 \times 10^5) — the number of test cases.

The first line of each test case contains two space-separated integers N,QN, Q (1QN5×1051 \le Q \le N \le 5 \times 10^5).

Each of the next N1N - 1 lines contains two space-separated integers u,vu, v — indicating that there is an edge connecting uu and vv in the tree. It is guaranteed that the N1N - 1 edges form a tree.

The next line contains QQ integers, K1,,KQK_1, \ldots, K_Q — denoting the QQ queries.

It is guaranteed that the sum of NN over all test cases in a test (denoted by N\sum N) does not exceed 5×1055 \times 10^5.

Output Format

For each test case, output one line with QQ space-separated integers — the values of f(K1),f(K2),,f(KQ)f(K_1), f(K_2), \ldots, f(K_Q), mod 109+710^9 + 7.

2
3 3
1 2
1 3
1 2 3
6 3
1 2
1 3
3 4
3 5
3 6
1 2 3
0 2 2
0 6 12

Hint

Explanation of Output for Sample Input

The two trees in the sample input are shown below.

:::align{center} :::

In the first test case, for K=2K = 2 or K=3K = 3, both [1,2,3][1, 2, 3] and [1,3,2][1, 3, 2] are good permutations. [2,1,3][2, 1, 3] is not a good permutation for all values of KK because

d(1,p1)=10=d(1,p2)d(1, p_1) = 1 \nleq 0 = d(1, p_2)

violates the second condition.

It can be shown that no permutation is good for K=1K = 1.

In the second test case, [1,3,2,4,5,6][1, 3, 2, 4, 5, 6] is a good permutation for K=3K = 3 but not a good permutation for K=2K = 2 because d(2,4)=32d(2, 4) = 3 \nleq 2.

The following table shows how the 2525 available marks are distributed:

Marks Awarded Bounds on N\sum N Bounds on QQ Bounds on KiK_i
22 marks 1N101 \le \sum N \le 10 1QN1 \le Q \le N 1KiN1 \le K_i \le N
33 marks 1N5×1051 \le \sum N \le 5 \times 10^5 1Qmin(2,N)1 \le Q \le \min(2, N) 1Kimin(2,N)1 \le K_i \le \min(2, N)
55 marks 1N30001 \le \sum N \le 3000 1Qmin(5,N)1 \le Q \le \min(5, N) 1KiN1 \le K_i \le N
77 marks 1N5×1051 \le \sum N \le 5 \times 10^5 ^
88 marks ^ 1QN1 \le Q \le N ^