No.2427 Tree Distance Two
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もしくは右上の雲マークをクリックしてアカウントを作成してください。
noya2
0214sh7