#P17283. 「IXOI R2」Retribution ~ Cycle of Redemption ~
「IXOI R2」Retribution ~ Cycle of Redemption ~
Background

Problem Description
There is a rooted tree with nodes, where the node labels are , and the root node is labeled .
:::info[Some function definitions related to this problem]{open}
For an array , denotes the smallest non-negative integer that does not appear in . For example, , and $\operatorname{mex}(\{\})=\operatorname{mex}(\{1,2,3\})=0$.
Define as the lowest common ancestor of nodes and in the tree.
For an array , is obtained by the following operations:
- Choose all unordered pairs from (here you may choose ), and add into an array .
- For , compute , which is .
:::
Next, there are independent queries. You need to choose some numbers from and put them into an array . In each query, you are given a parameter , meaning you must choose at least distinct numbers to put into . Among all that satisfy the requirement, find the minimum possible value of .
Input Format
The first line contains three integers .
The next lines each contain two integers , indicating an edge in the tree.
The next lines each contain one integer , which is the parameter of the query.
Output Format
Output lines, each containing one integer.
The integer on line is the answer to the -th query.
9 6 2
4 5
4 6
4 0
1 0
1 7
1 8
2 0
2 3
9
8
7
6
5
4
9
3
1
1
0
0
Hint
Sample Explanation
For the first query, one valid choice is . Then . It can be proven that there is no better choice.
For the sixth query, when , we have . It can be proven that there is no better choice.
Constraints
This problem uses bundled testdata.
| Subtask | Special property | Score | |
|---|---|---|---|
| None | |||
| and node is directly connected to all other nodes | |||
| None |
For all testdata, it is guaranteed that , , and .
Translated by ChatGPT 5