問題一覧 > 通常問題

No.1976 Cut then Connect

レベル : / AI推定難易度 : 1975 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 41
作問者 : magsta / テスター : shino16
お気に入りにしたユーザー ProblemId : 7942 / 自分の提出
問題文最終更新日: 2026-10-22 18:00:18
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
yukicoder contest 347 (順位表) の他の問題:

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もしくは右上の雲マークをクリックしてアカウントを作成してください。