#C1395. Tree Diameter
Tree Diameter
Tree Diameter
Given a tree with (n) nodes labeled from 1 to (n) and (n-1) edges where each edge connects two nodes, compute the tree diameter. The tree diameter is defined as the maximum distance (i.e., the maximum number of edges) between any two nodes in the tree. In mathematical terms, the diameter is given by: [ \max_{u,v \in V} d(u,v)] where (d(u,v)) is the distance between nodes (u) and (v). You can solve this problem by performing two breadth-first searches (BFS).
inputFormat
The input is read from standard input (stdin). The first line contains a single integer (n) ((n \ge 2)), representing the number of nodes in the tree. Each of the following (n-1) lines contains two space-separated integers (u) and (v), representing an edge between nodes (u) and (v).
outputFormat
Output a single integer to standard output (stdout) representing the diameter of the tree (i.e., the number of edges in the longest path between any two nodes).## sample
4
1 2
1 3
3 4
3
</p>