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

    ID: 19625 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>贪心二分洛谷原创O2优化树形 DP洛谷月赛双指针 two-pointer

「IXOI R2」Retribution ~ Cycle of Redemption ~

Background

Problem Description

There is a rooted tree with nn nodes, where the node labels are 0n10 \sim n-1, and the root node is labeled rr.

:::info[Some function definitions related to this problem]{open}

For an array SS, mex(S)\operatorname{mex}(S) denotes the smallest non-negative integer that does not appear in SS. For example, mex({0,1,2,4,5})=3\operatorname{mex}(\{0,1,2,4,5\})=3, and $\operatorname{mex}(\{\})=\operatorname{mex}(\{1,2,3\})=0$.

Define lca(x,y)\operatorname{lca}(x,y) as the lowest common ancestor of nodes xx and yy in the tree.

For an array SS, lcx(S)\operatorname{lcx}(S) is obtained by the following operations:

  1. Choose all unordered pairs (x,y)(x,y) from SS (here you may choose x=yx=y), and add lca(x,y)\operatorname{lca}(x,y) into an array TT.
  2. For TT, compute mex(T)\operatorname{mex}(T), which is lcx(S)\operatorname{lcx}(S).

:::

Next, there are qq independent queries. You need to choose some numbers from 0n10 \sim n-1 and put them into an array HH. In each query, you are given a parameter xx, meaning you must choose at least xx distinct numbers to put into HH. Among all HH that satisfy the requirement, find the minimum possible value of lcx(H)\operatorname{lcx}(H).

Input Format

The first line contains three integers n,q,rn,q,r.

The next n1n-1 lines each contain two integers u,vu,v, indicating an edge in the tree.

The next qq lines each contain one integer xx, which is the parameter of the query.

Output Format

Output qq lines, each containing one integer.

The integer on line ii is the answer to the ii-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 H={0,1,2,3,4,5,6,7,8}H=\{0,1,2,3,4,5,6,7,8\}. Then lcx(H)=9\operatorname{lcx}(H)=9. It can be proven that there is no better choice.

For the sixth query, when H={3,4,5,6}H=\{3,4,5,6\}, we have lcx(H)=0\operatorname{lcx}(H)=0. It can be proven that there is no better choice.

Constraints

This problem uses bundled testdata.

Subtask nn\le Special property Score
11 1010 None 1010
22 10310^3 2525
33 10610^6 r=1r=1 and node 00 is directly connected to all other nodes
44 None 4040

For all testdata, it is guaranteed that 0u,v,r<n1060\le u,v,r<n\le 10^6, 1q106+11\le q\le 10^6+1, and 0xn0\le x\le n.

Translated by ChatGPT 5