No.1817 Reversed Edges
タグ : / 解いたユーザー数 154
作問者 :
nok0
/ テスター :
kichi2004_
riano
Problem Statement
You are given a tree with $N$ vertices.
The vertices are numbered from $1$ to $N$ and the edges are numbered from $1$ to $N-1$. Edge $i$ bidirectionally connects vertices $A_i$ and $B_i$.
The contrarian degree of a vertex $x$ is defined as follows.
- Root the tree at vertex $x$ and orient each edge of the tree in the direction away from vertex $x$. Let $s_i$ be the start point and $t_i$ the end point of each directed edge $i$ obtained by this orientation. The contrarian degree of vertex $x$ is defined as the number of $i$ satisfying $s_i > t_i$.
Find the contrarian degrees of vertices $1,2,\dots,N$.
Constraints
- All inputs are integers.
- $2 \le N \le 10^5$
- $1 \le A_i,B_i \le N$
- The given graph is a tree.
Input
$N$
$A_1$ $B_1$
$A_2$ $B_2$
$\vdots$
$A_{N-1}$ $B_{N-1}$
Output
On the $i\ (1\le i \le N)$-th line, output the contrarian degree of vertex $i$.
Sample
Sample 1
Input
3 1 2 2 3
Output
0 1 2
Consider the contrarian degree of vertex $1$. Orienting the edges away from vertex $1$, edge $1$ becomes a directed edge from vertex $1$ to $2$, and edge $2$ becomes a directed edge from vertex $2$ to $3$. The number of edges whose start point has a larger number than its end point is $0$, so the contrarian degree of vertex $1$ is $0$.
Consider the contrarian degree of vertex $2$. Orienting the edges away from vertex $2$, edge $1$ becomes a directed edge from vertex $2$ to $1$, and edge $2$ becomes a directed edge from vertex $2$ to $3$. The number of edges whose start point has a larger number than its end point is $1$, so the contrarian degree of vertex $2$ is $1$.
Consider the contrarian degree of vertex $3$. Orienting the edges away from vertex $3$, edge $1$ becomes a directed edge from vertex $2$ to $1$, and edge $2$ becomes a directed edge from vertex $3$ to $2$. The number of edges whose start point has a larger number than its end point is $2$, so the contrarian degree of vertex $3$ is $2$.
Sample 2
Input
5 1 2 1 4 2 3 2 5
Output
0 1 2 1 2
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。