#P11195. [COTS 2021] 疫苗接种 Cijepise

[COTS 2021] 疫苗接种 Cijepise

Background

Translated from Izborne Pripreme 2021 (Croatian IOI/CEOI Team Selection) D2T1。5s,1G\texttt{5s,1G}

Problem Description

You are given a rooted tree with NN nodes, rooted at 11. Let the value (weight) of node ii be aia_i.

Define one operation as follows:

  • Initialize uu as the root of the tree.
    1. Let vv be the child of uu with the largest value (if there are multiple, choose one uniformly at random). Set auava_u\gets a_v and av0a_v\gets 0.
    2. If vv is a leaf, delete vv and terminate this process. Otherwise set uvu\gets v and go back to step 1.

Define f(v)f(v) as: in the worst case, in order to make ava_v 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 vv needs kk operations to reach the root. Obviously, when the tree shape is fixed, different node values will make kk take different values, and kk has a lower bound. You need to modify the values of some nodes so that kk reaches this lower bound, while minimizing the number of modifications.

A node value can be changed to any integer in [0,2×109][0,2\times 10^9].

There are QQ queries. Given vv, output f(v)f(v).

Input Format

The first line contains one positive integer NN.

The second line contains NN positive integers aia_i.

The next (N1)(N-1) lines each contain two positive integers u,vu,v, describing an edge (u,v)(u,v) in the tree.

The next line contains one positive integer QQ.

The next QQ lines each contain one positive integer vv, representing a query for f(v)f(v).

Output Format

Output QQ 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 100%100\% of the testdata, it is guaranteed that 1QN1×1051\le Q\le N\le 1\times 10^5, 1ai1091\le a_i\le 10^9, 1vN1\le v\le N. The input is a tree.

Constraints

Subtask ID NN\le Special Property Score
11 1212 None 1010
22 300300 1111
33 30003\,000 Yes 1212
44 None 1313
55 100000100\,000 Yes 2020
66 None 3434

Special Property: Q1Q\le 1

Translated by ChatGPT 5