問題一覧 > 通常問題

No.1098 LCAs

レベル : / AI推定難易度 : 950 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 202
作問者 : SSRS / テスター : しの
お気に入りにしたユーザー ProblemId : 4402 / 自分の提出
問題文最終更新日: 2020-10-14 23:43:10
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 254 (順位表) の他の問題:

Problem Statement

There is a rooted tree with $N$ vertices.
The vertices of the rooted tree are numbered from $1$ to $N$, and the root is vertex $1$. The $i (1 \leq i < N)$-th edge connects vertex $v_i$ and vertex $w_i$ bidirectionally.
For each $k=1,2,\cdots,N$, solve the following problem.

  • Find the number of pairs $(v,w)$ of two vertices of the tree whose lowest common ancestor (LCA) is vertex $k$.

Input

$N$
$v_1 \ w_1$
$v_2 \ w_2$
$\vdots$
$v_{N-1} \ w_{N-1}$

The input satisfies the following constraints.

  • All input values are integers.
  • $1 \leq N \leq 2\times10^5$
  • $1 \leq v_i, w_i \leq N (1 \leq i < N)$
  • The given graph is a tree.

Output

Output $N$ lines.
On the $i (1 \leq i \leq N)$-th line, output the answer for $k=i$.

Sample

Sample 1
Input
3
1 2
1 3
Output
7
1
1

The pairs $(v,w)$ such that the LCA of vertex $v$ and vertex $w$ is vertex $1$ are the $7$ pairs $(1,1), (1,2), (1,3), (2,1), (2,3), (3,1), (3,2)$.
The only pair $(v,w)$ such that the LCA of vertex $v$ and vertex $w$ is vertex $2$ is the $1$ pair $(2,2)$.
The only pair $(v,w)$ such that the LCA of vertex $v$ and vertex $w$ is vertex $3$ is the $1$ pair $(3,3)$.
Therefore, the answers for $k=1,2,3$ are $7,1,1$, respectively.

Sample 2
Input
5
1 2
2 3
3 4
3 5
Output
9
7
7
1
1

Sample 3
Input
1
Output
1

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