問題一覧 > 通常問題

No.2427 Tree Distance Two

レベル : / AI推定難易度 : 750 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 161
作問者 : noya2 / テスター : 0214sh7 ebi_fly 👑 potato167 tassei903 ramdos ponjuice
お気に入りにしたユーザー ProblemId : 9997 / 自分の提出
問題文最終更新日: 2023-08-11 13:13:01
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 作問ハッカソンコンテスト 001 (順位表) の他の問題:

Problem Statement

You are given a tree with $N$ vertices numbered $1,2,\dots ,N$. For $i=1,2,\dots ,N-1$, the $i$-th edge connects vertex $u_i$ and vertex $v_i$.

For each $v=1,2,\dots ,N$, find the number of vertices whose distance from vertex $v$ is $2$.

Constraints

  • All input values are integers.
  • $2\le N\le 3\times 10^5$
  • $1\le u_i,v_i\le N\ (1\le i\le N-1)$
  • The given graph is a tree.

Input

$N$
$u_1$ $v_1$
$\vdots$
$u_{N-1}$ $v_{N-1}$

Output

Print $N$ lines. On the $v$-th line, print the number of vertices whose distance from vertex $v$ is $2$.

Sample

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

There are $4$ pairs of vertices at distance $2$: $(1,3),(2,4),(2,5),(4,5)$.

Sample 2
Input
2
1 2
Output
0
0

There are no pairs of vertices at distance $2$.

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

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