#abc218g. G - Game on Tree 2

G - Game on Tree 2

Score : 600600 points

问题描述

我们有一棵包含 NN 个顶点的树,顶点编号从 11NN。第 ii 条边(1iN11 \leq i \leq N-1)连接顶点 uiu_i 和顶点 viv_i,且第 ii 个顶点(1iN1 \leq i \leq N)上写着一个偶数 AiA_i。太郎和次郎将通过这棵树和一颗棋子进行一场游戏。

初始时,棋子位于顶点 11 上。由太郎先手,两名玩家交替将棋子移动到与当前所在顶点直接相连的顶点上。但是,棋子不能重访已经访问过的顶点。当棋子无法再移动时,游戏结束。

太郎希望最大化游戏结束前棋子所访问过(包括顶点 11 在内)的所有顶点上的数字的中位数,而次郎则希望使这个值最小化。在两位玩家都采取最优策略的情况下,求出这个集合的中位数。

什么是中位数?

对于包含 KK 个数字(可以有重复)的集合,中位数定义如下:

  • KK 是奇数时,中位数是第 K+12\frac{K+1}{2} 小的数字;
  • KK 是偶数时,中位数是第 K2\frac{K}{2} 小和第 (K2+1)(\frac{K}{2}+1) 小的两个数字的平均值。

例如,集合 {2,2,4}\{ 2,2,4 \} 的中位数是 22,而集合 {2,4,6,6}\{ 2,4,6,6\} 的中位数是 55

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

We have a tree with NN vertices numbered 11 through NN. The ii-th edge (1iN1)(1 \leq i \leq N-1) connects Vertex uiu_i and Vertex viv_i, and Vertex ii (1iN)(1 \leq i \leq N) has an even number AiA_i written on it. Taro and Jiro will play a game using this tree and a piece.

Initially, the piece is on Vertex 11. 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 11), 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 KK numbers is defined as follows:

  • the K+12\frac{K+1}{2}-th smallest number, if KK is odd;
  • the average of the K2\frac{K}{2}-th and (K2+1)(\frac{K}{2}+1)-th smallest numbers, if KK is even.

For example, the median of {2,2,4}\{ 2,2,4 \} is 22, and the median of {2,4,6,6}\{ 2,4,6,6\} is 55.

Constraints

  • 2N1052 \leq N \leq 10^5
  • 2Ai1092 \leq A_i \leq 10^9
  • AiA_i is even.
  • 1ui<viN1 \leq u_i < v_i \leq N
  • The given graph is a tree.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \ldots ANA_N

u1u_1 v1v_1

u2u_2 v2v_2

\vdots

uN1u_{N-1} vN1v_{N-1}

Output

Print the median of the (multi-)set of numbers written on the vertices visited by the piece when both players play optimally.

Sample Input 1

5
2 4 6 8 10
4 5
3 4
1 5
2 4

Sample Output 1

7

When both players play optimally, the game goes as follows.

  • Taro moves the piece from Vertex 11 to Piece 55.
  • Jiro moves the piece from Vertex 55 to Piece 44.
  • Taro moves the piece from Vertex 44 to Piece 33.
  • Jiro cannot move the piece, so the game ends.

Here, the set of numbers written on the vertices visited by the piece is {2,10,8,6}\{2,10,8,6\}. The median of this set is 77, which should be printed.

Sample Input 2

5
6 4 6 10 8
1 4
1 2
1 5
1 3

Sample Output 2

8

When both players play optimally, the game goes as follows.

  • Taro moves the piece from Vertex 11 to Piece 44.
  • Jiro cannot move the piece, so the game ends.

Here, the set of numbers written on the vertices visited by the piece is {6,10}\{6,10\}. The median of this set is 88, which should be printed.

Sample Input 3

6
2 2 6 4 6 6
1 2
2 3
4 6
2 5
2 6

Sample Output 3

2

update @ 2024/3/10 09:39:48