#C6907. Tree Diameter

    ID: 50719 Type: Default 1000ms 256MiB

Tree Diameter

Tree Diameter

You are given a tree with n nodes labeled from 1 to n. A tree is an undirected connected graph with no cycles. The tree is described by its n - 1 edges. Your task is to compute the diameter of the tree.

The diameter of a tree is the number of edges in the longest path between any two nodes. Mathematically, if we denote the path length between any two nodes u and v as d(u, v), then the diameter is defined as:

\(\max_{u,v \in V} \; d(u,v)\)

You can solve this problem by performing two breadth-first searches (BFS):

  1. Run BFS starting from an arbitrary node to find the farthest node u.
  2. Run BFS starting from u to determine the farthest node v. The distance between u and v is the diameter of the tree.

inputFormat

The first line contains a single integer n (2 ≤ n ≤ 105), representing the number of nodes in the tree. Each of the following n - 1 lines contains two integers u and v (1 ≤ u, v ≤ n), representing an undirected edge between nodes u and v.

outputFormat

Output a single integer — the diameter of the tree.

## sample
5
1 2
1 3
2 4
2 5
3