#C10663. Tree Diameter
Tree Diameter
Tree Diameter
You are given a tree with N nodes. The tree is provided as an undirected acyclic graph. Your task is to compute the diameter of the tree, which is defined as the length (i.e., the number of edges) of the longest path between any two nodes.
The input consists of an integer N representing the number of nodes, followed by N - 1 lines each containing two integers u and v indicating that there is an edge connecting nodes u and v.
Note: When N = 1, the tree has a single node and its diameter is 0.
The diameter can be computed by performing two breadth-first searches (BFS): the first to find one endpoint of the longest path, and the second starting from that endpoint to determine the distance to the farthest node.
inputFormat
The first line of input contains a single integer N (1 ≤ N). The following N - 1 lines each contain two space-separated integers u and v (1 ≤ u, v ≤ N) representing an edge of the tree.
outputFormat
Output a single integer representing the diameter of the tree.
## sample5
1 2
1 3
3 4
3 5
3
</p>