#P10060. [SNOI2024] 树 V 图

[SNOI2024] 树 V 图

Problem Description

You are given an unrooted tree with nn nodes, numbered 1,2,,n1, 2, \ldots, n. Define the distance dis(i,j)\operatorname{dis}(i, j) between two nodes on the tree as the number of edges on the simple path from node ii to node jj.

Now there are kk key nodes a1,a2,,aka_1, a_2, \ldots, a_k. For each node, we want to find which key node is closest to it. That is, for a node vv, let f(v)f(v) be the index ii that minimizes dis(v,ai)\operatorname{dis}(v, a_i). If multiple indices ii satisfy this, choose the smallest such ii.

Now you are given f(1),f(2),,f(n)f(1), f(2), \ldots, f(n). How many possible tuples (a1,a2,,ak)(a_1, a_2, \ldots, a_k) satisfy the condition? Since the answer may be large, output it modulo 998244353998244353.

Input Format

Multiple test cases. The first line contains an integer TT, the number of test cases.
For each test case, the first line contains two integers n,kn, k, the number of nodes and the number of key nodes.
The next n1n - 1 lines each contain two integers u,vu, v, representing an edge (u,v)(u, v) of the tree.
The next line contains nn integers f(1),f(2),,f(n)f(1), f(2), \ldots, f(n). Note: the testdata is not guaranteed to have at least one valid tuple (a1,a2,,ak)(a_1, a_2, \ldots, a_k).

Output Format

For each test case, output one integer: the answer modulo 998244353998244353.

3
3 3
1 2
2 3
1 2 1
3 2
1 2
2 3
1 2 2
3 2
1 2
2 3
2 1 1

0
1
2

1
10 5
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
1 1 2 2 3 3 4 4 5 5

13

Hint

[Sample #1 Explanation]

In the first sample, for the second test case, one solution is (1,2)(1, 2). For the third test case, there are two solutions: (2,1),(3,1)(2, 1), (3, 1).

Note that when multiple nodes have the same distance, we choose the smallest index ii, not the smallest aia_i.


[Sample #3]

See the attached files voronoi/voronoi3.in and voronoi/voronoi3.ans. This sample satisfies the constraints of test points 343 \sim 4.


[Sample #4]

See the attached files voronoi/voronoi3.in and voronoi/voronoi3.ans. This sample satisfies the constraints of test points 7107 \sim 10.


[Constraints]

For all testdata, it is guaranteed that 1T101 \le T \le 10, 2kn3×1032 \le k \le n \le 3 \times {10}^3, and 1f(i)k1 \le f(i) \le k.

Details:

Test Point ID nn \le Special Property
121 \sim 2 1515 None
343 \sim 4 30003000 A
565 \sim 6 B
7107 \sim 10 None

Special Property A: the tree is guaranteed to be a chain.
Special Property B: k=2k = 2 is guaranteed.

Translated by ChatGPT 5