No.1976 Cut then Connect
Problem Statement
You are given a tree with $N$ vertices. The vertices of the tree are numbered from $1$ to $N$, and the edges are numbered from $1$ to $N-1$.
Edge $i \ (1 \leq i \leq N-1)$ connects vertices $u_i, v_i$.
Now, perform the following operation.
Remove $1$ of the undirected edges of the given tree. The number of connected components of the graph becomes $2$.
For each connected component of the graph, choose $1$ vertex belonging to that component, and connect those $2$ vertices with an undirected edge.
The graph remains a tree after this operation. Find the minimum possible diameter of the tree after the operation.
Here, the diameter of a tree is defined as the maximum number of edges contained in a path connecting $2$ vertices in the tree.
Constraints
- $\displaystyle 2 \leq N \leq 10^5$
- $\displaystyle 1 \leq u_i, v_i \leq N$
- $\displaystyle u_i \neq v_i$
- All inputs are integers.
- The graph represented by the input is a tree.
Input
The input is given from standard input in the following format.
$N$
$u_1\ \ v_1$
$u_2\ \ v_2$
$:$
$u_{N-1}\ \ v_{N-1}$
Output
Output the value you found, followed by a newline.
Sample
Sample 1
Input
5 1 2 2 3 3 4 4 5
Output
3
Removing the edge connecting vertex $2$ and vertex $3$ and connecting vertex $2$ and vertex $4$ with an edge makes the diameter $3$.
The diameter cannot be made $2$ or less, so this is the minimum value.
Sample 2
Input
2 1 2
Output
1
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
magsta
shino16