#FT0000185. Trò chơi trung vị trên cây (Game on Tree 2)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Trò chơi trung vị trên cây (Game on Tree 2)
Game on Tree 2
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
An undirected tree has vertices, with an even integer written on each vertex. A token starts at vertex . Taro moves first; Jiro and Taro then alternate. A player must move the token to an adjacent vertex not visited before. The game ends when no move is available. Consider the multiset of values on all visited vertices, including vertex . For an odd number , the median is the -th value in sorted order; for even , it is the average of the -th and -th values. Taro maximizes and Jiro minimizes the final median. Find the result under optimal play. All values are even, so the median is integral.
Input
The first line contains ; the second contains . The next lines contain undirected edges u v with .
Output
Print one integer: the final median under optimal play.
Subtasks
- Subtask 1 (20 points): ; and every is even; exactly edges
u vwith form a tree. - Subtask 2 (30 points): ; and every is even; exactly edges
u vwith form a tree. - Subtask 3 (50 points): ; and every is even; exactly edges
u vwith form a tree.
Examples
Example 1
Input
5
2 4 6 8 10
4 5
3 4
1 5
2 4
Output
7
Explanation
At leaf 3, path [1, 5, 4, 3], sorted values [2, 6, 8, 10], median = 7. At leaf 2, path [1, 5, 4, 2], sorted values [2, 4, 8, 10], median = 6. At vertex 4, Taro (max) chooses among child values [7, 6], obtaining 7. At vertex 5, Jiro (min) chooses among child values [7], obtaining 7. At vertex 1, Taro (max) chooses among child values [7], obtaining 7.
Example 2
Input
5
6 4 6 10 8
1 4
1 2
1 5
1 3
Output
8
Explanation
At leaf 4, path [1, 4], sorted values [6, 10], median = 8. At leaf 2, path [1, 2], sorted values [4, 6], median = 5. At leaf 5, path [1, 5], sorted values [6, 8], median = 7. At leaf 3, path [1, 3], sorted values [6, 6], median = 6. At vertex 1, Taro (max) chooses among child values [8, 5, 7, 6], obtaining 8.