atcoder#ABC218G. [ABC218G] Game on Tree 2
[ABC218G] Game on Tree 2
Score : points
Problem Statement
We have a tree with vertices numbered through . The -th edge connects Vertex and Vertex , and Vertex has an even number written on it. Taro and Jiro will play a game using this tree and a piece.
Initially, the piece is on Vertex . With Taro going first, the players alternately move the piece to a vertex directly connected to the vertex on which the piece lies. However, the piece must not revisit a vertex it has already visited. The game ends when the piece gets unable to move.
Taro wants to maximize the median of the (multi-)set of numbers written on the vertices visited by the piece before the end of the game (including Vertex ), and Jiro wants to minimize this value. Find the median of this set when both players play optimally.
What is median?
The median of a (multi-)set of numbers is defined as follows:
- the -th smallest number, if is odd;
- the average of the -th and -th smallest numbers, if is even.
For example, the median of is , and the median of is .
Constraints
- is even.
- The given graph is a tree.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the median of the (multi-)set of numbers written on the vertices visited by the piece when both players play optimally.
5
2 4 6 8 10
4 5
3 4
1 5
2 4
7
When both players play optimally, the game goes as follows.
- Taro moves the piece from Vertex to Piece .
- Jiro moves the piece from Vertex to Piece .
- Taro moves the piece from Vertex to Piece .
- Jiro cannot move the piece, so the game ends.
Here, the set of numbers written on the vertices visited by the piece is . The median of this set is , which should be printed.
5
6 4 6 10 8
1 4
1 2
1 5
1 3
8
When both players play optimally, the game goes as follows.
- Taro moves the piece from Vertex to Piece .
- Jiro cannot move the piece, so the game ends.
Here, the set of numbers written on the vertices visited by the piece is . The median of this set is , which should be printed.
6
2 2 6 4 6 6
1 2
2 3
4 6
2 5
2 6
2