Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/daraxt-diametri

Tree diameter

Medium 1000 ms 128 MB 60%
Share

A tree (a connected acyclic graph) with $n$ vertices and $n - 1$ edges is given. The vertices are numbered from $1$ to $n$. Find the diameter of the tree — the number of edges on the longest simple path between two vertices.

A tree with its longest path highlighted

Input

The first line contains a positive integer $n$. Each of the next $n - 1$ lines contains two positive integers: $u$ and $v$ — the vertices joined by an edge.

Output

One integer — the diameter of the tree.

Note

$1 \le n \le 2 * 10^{5}$
$1 \le u, v \le n$

Examples

Input #1
5
1 2
1 3
2 4
2 5
Output #1
3
Input #2
4
1 2
2 3
3 4
Output #2
3

Sign in to submit a solution

Signing up is free and takes a minute.