No.806 Turning a Tree into a Path
タグ : / 解いたユーザー数 355
作問者 :
sinsincoscos
/ テスター :
keymoon
Problem Statement
umg has an undirected tree with $N$ vertices. The $i$-th edge connects vertices $a_i$ and $b_i$, and we denote it as edge $(a_i,b_i)$. He can perform the following operation.
Operation: Choose three distinct vertices $u,v,w$ and change edge $(u,v)$ to edge $(u,w)$.
umg wants to perform this operation some number of times to turn his tree into a path. A path is an undirected tree that has exactly two vertices of degree 1.
Find the minimum number of operations umg needs to achieve his goal.
Input
$N$
$a_1\ b_1$
$\vdots$
$a_{N-1}\ b_{N-1}$
$3\le N \le 10^5$
$1\le a_i , b_i \le N$
The given graph is a tree.
For $i\neq j$, $(a_i,b_i)\neq (a_j,b_j)$.
Output
Output the answer on a single line. Print a newline at the end.
Sample
Sample 1
Input
4 1 2 1 3 1 4
Output
1
You can achieve the goal by changing edge $(2,1)$ to edge $(2,4)$.
Sample 2
Input
3 1 2 2 3
Output
0
The tree is already a path from the start, so no operations are needed.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。