#P10060. [SNOI2024] 树 V 图
[SNOI2024] 树 V 图
Problem Description
You are given an unrooted tree with nodes, numbered . Define the distance between two nodes on the tree as the number of edges on the simple path from node to node .
Now there are key nodes . For each node, we want to find which key node is closest to it. That is, for a node , let be the index that minimizes . If multiple indices satisfy this, choose the smallest such .
Now you are given . How many possible tuples satisfy the condition? Since the answer may be large, output it modulo .
Input Format
Multiple test cases. The first line contains an integer , the number of test cases.
For each test case, the first line contains two integers , the number of nodes and the number of key nodes.
The next lines each contain two integers , representing an edge of the tree.
The next line contains integers . Note: the testdata is not guaranteed to have at least one valid tuple .
Output Format
For each test case, output one integer: the answer modulo .
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 . For the third test case, there are two solutions: .
Note that when multiple nodes have the same distance, we choose the smallest index , not the smallest .
[Sample #3]
See the attached files voronoi/voronoi3.in and voronoi/voronoi3.ans. This sample satisfies the constraints of test points .
[Sample #4]
See the attached files voronoi/voronoi3.in and voronoi/voronoi3.ans. This sample satisfies the constraints of test points .
[Constraints]
For all testdata, it is guaranteed that , , and .
Details:
| Test Point ID | Special Property | |
|---|---|---|
| None | ||
| A | ||
| B | ||
| None |
Special Property A: the tree is guaranteed to be a chain.
Special Property B: is guaranteed.
Translated by ChatGPT 5