#P11195. [COTS 2021] 疫苗接种 Cijepise
[COTS 2021] 疫苗接种 Cijepise
Background
Translated from Izborne Pripreme 2021 (Croatian IOI/CEOI Team Selection) D2T1。。
Problem Description
You are given a rooted tree with nodes, rooted at . Let the value (weight) of node be .
Define one operation as follows:
- Initialize as the root of the tree.
- Let be the child of with the largest value (if there are multiple, choose one uniformly at random). Set and .
- If is a leaf, delete and terminate this process. Otherwise set and go back to step 1.

Define as: in the worst case, in order to make appear at the root as fast as possible (i.e., using as few operations as possible), what is the minimum number of nodes whose values must be changed (you may also choose not to change any).
In other words, in the worst case, node needs operations to reach the root. Obviously, when the tree shape is fixed, different node values will make take different values, and has a lower bound. You need to modify the values of some nodes so that reaches this lower bound, while minimizing the number of modifications.
A node value can be changed to any integer in .
There are queries. Given , output .
Input Format
The first line contains one positive integer .
The second line contains positive integers .
The next lines each contain two positive integers , describing an edge in the tree.
The next line contains one positive integer .
The next lines each contain one positive integer , representing a query for .
Output Format
Output lines, each being the answer to one query.
3
1 2 3
1 2
1 3
3
1
2
3
0
1
0
7
45 13 19 3 81 27 77
1 2
1 3
1 4
3 5
3 6
4 7
3
5
6
7
0
1
1
8
23 4 9 7 11 4 1 18
2 1
3 2
4 2
5 2
6 3
7 4
8 1
3
2
3
7
1
2
3
Hint
For of the testdata, it is guaranteed that , , . The input is a tree.
Constraints
| Subtask ID | Special Property | Score | |
|---|---|---|---|
| None | |||
| Yes | |||
| None | |||
| Yes | |||
| None |
Special Property: 。
Translated by ChatGPT 5