問題一覧 > 通常問題

No.899 γatheree

レベル : / AI推定難易度 : 2025 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 59
作問者 : niuez / テスター : Lemma299 polylogK
お気に入りにしたユーザー ProblemId : 3405 / 自分の提出
問題文最終更新日: 2019-10-05 00:11:08
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
yukicoder contest 226 (木上クエリコン) (順位表) の他の問題:

Problem Statement

The sacred tree in niuez's town is home to tree fairies.
When a "tri-butree" is placed on a vertex of a tree, the fairies nearby gather at that vertex.
niuez decided to play with this.

There is an undirected tree with $N$ vertices numbered from $0$ to $N-1$ and $N-1$ edges, where the $i \ (0 \leq i < N-1)$-th edge has endpoints $u_i$ and $v_i$.
Also, there are $A_i$ fairies on vertex $i \ (0 \leq i \le N - 1)$.
Fairies stay only on vertices, and they have the habit that when a "tri-butree" is placed on a vertex at distance $2$ or less from the vertex where they are staying, they move to the vertex where the "tri-butree" was placed.
After moving, the fairies stay on the destination vertex.
Here, the distance between a pair of vertices $a, b$ is defined as the number of edges on the path between $a, b$.
Process the following $Q$ ${\rm query}$ in order.

  • $x$
    • Place a "tri-butree" on vertex $x$. Then, find the number of fairies on vertex $x$.

Input

$N$
$u_0 \  v_0$
$\cdots$
$u_{N-2} \  v_{N-2}$
$A_0 \  A_1 \ \cdots \  A_{N-1}$
$Q$
$x_0$
$\cdots$
$x_{Q-1}$
  • $2 \leq N \leq 10^5$
  • $0 \leq u_i, v_i < N$
    • $u_i \neq v_i$
    • $(u_i, v_i) = (u_j, v_j) \Leftrightarrow i = j$
  • $0 \leq A_i \leq 10^5$
  • $1 \leq Q \leq 10^5$
  • $0 \leq x_i < N$
  • All input values are integers.

Output

Output $Q$ lines. On the $i$-th line, output the answer to the $i$-th ${\rm query}$ as an integer.

Sample

Sample 1
Input
10
0 1
0 2
1 3
1 4
2 5
2 6
3 7
3 8
4 9
0 1 2 3 4 5 6 7 8 9
3
1
4
0
Output
34
34
45

The vertices at distance $2$ or less from vertex $1$ are $0, \ 1, \ 2, \ 3, \ 4, \ 7, \ 8, \ 9$.
Therefore, $0+1+2+3+4+7+8+9=34$ fairies gather at vertex $1$.
Also, for the last query, on vertex $0$, all the fairies on the tree gather at vertex $0$.

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