#P16560. [ICPC 2026 APC] Subtree Removal Game
[ICPC 2026 APC] Subtree Removal Game
题目描述
You are given a rooted tree with nodes, numbered from to , rooted at node . Foar each node ( ), its parent is node . For each node , if it is a leaf (a node having no children), the integer is written on it. Otherwise, nothing is written.
Node is a descendant of node if , or is not the root and the parent of is a descendant of .
Now, you and your friend play a game on this tree, taking turns alternately: You move first, then your friend, and so on. On each turn, the current player must choose a node and then remove the entire subtree of (i.e., all descendants of , including itself). The move is allowed only if, after the removal, at least one node with an integer written on it remains in the tree.
The game ends when no further moves are possible. At that point, exactly one node with an integer written on it remains; the score of the game is the integer on that node.
You aim to minimize the score, while your friend aims to maximize it. Assuming you and your friend play optimally, determine the score of the game.
输入格式
The first line of input contains an integer ( ).
The second line contains integers ( for all ).
输出格式
Output the score of the game assuming you and your friend play optimally.
7
1 2 2 1 5 5
4
提示
Explanation for the sample input/output #1
The given tree is illustrated in Figure B.1 (a).
Figure B.1: Illustration of Sample Input #1.The only optimal moves for both players are as follows:
- You choose node . Then nodes , , and are removed (Figure B.1 (b)).
- Your friend chooses node . Then only node is removed (Figure B.1 (c)).
- You cannot make any moves, and the game ends.
After these moves, only one node with an integer written on it, node , remains. The score of this game is .Explanation for the sample input/output #2
The given tree is illustrated in Figure B.2. One of the optimal moves for you is to choose node . The game ends after that, and the score is .
Figure B.2: Illustration of Sample Input #2.