問題一覧 > 通常問題

No.2634 Tree Distance 3

レベル : / AI推定難易度 : 2125 / 実行時間制限 : 1ケース 3.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 31
作問者 : ebi_fly / テスター : noya2 👑 potato167
お気に入りにしたユーザー ProblemId : 10661 / 自分の提出
問題文最終更新日: 2024-02-16 19:34:30
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
traP 作問ハッカソンコンテスト 002(day1) (順位表) の他の問題:

Problem Statement

You are given a tree $T$ with $N$ vertices numbered from $1$ to $N$. The $i$-th edge connects vertices $u_i$ and $v_i$. Also, $A_i$ is written on vertex $i$. Solve the following problem for each $i = 1,2,\dots,N$.

  • Among all $j$ ( $1\leq j \leq N$) such that $A_i \leq A_j$, find the maximum value of the distance $d(i, j)$ between the $2$ vertices $i$, $j$ in the tree $T$.

Here, the distance $d(i, j)$ between $2$ vertices $i$, $j$ of a tree is the number of edges contained in the path whose endpoints are $i$ and $j$.

Constraints

  • $2 \leq N \leq 2\times 10^5$
  • $0 \leq A_i \leq 10^9$
  • $1 \leq u_i, v_i \leq N$
  • The graph given in the input is a tree.
  • All input values are integers.

Input

The input is given from standard input in the following format.

$N$
$A_1$ $A_2$ $\dots$ $A_N$
$u_1$ $v_1$
$u_2$ $v_2$
$\vdots$
$u_{N-1}$ $v_{N-1}$

Output

Output the answers for $i = 1,2,\dots,N$, separated by spaces.

Sample

Sample 1
Input
5
4 2 2 3 1
1 2
1 3
3 4
3 5
Output
0 3 2 2 3

For $i = 1$, the only possible value of $j$ is $1$, so the answer is $0$.

For $i = 2$, the possible values of $j$ are $1$, $2$, $3$, $4$, and the answer is $3$, attained when $j = 4$.

For $i = 3$, the possible values of $j$ are $1$, $2$, $3$, $4$, and the answer is $2$, attained when $j = 2$.

For $i = 4$, the possible values of $j$ are $1$, $4$, and the answer is $2$, attained when $j = 1$.

For $i = 5$, the possible values of $j$ are $1$, $2$, $3$, $4$, $5$, and the answer is $3$, attained when $j = 2$.

Sample 2
Input
6
2 9 1 4 6 8
1 2
1 3
1 4
3 5
3 6
Output
2 0 2 3 3 3

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。