#K83797. Tree Diameter
Tree Diameter
Tree Diameter
You are given a tree with (N) vertices and (N-1) edges. A tree is an undirected, connected, acyclic graph. The diameter of a tree is defined as the length of the longest path between any two nodes. Formally, if (d(u,v)) represents the distance (number of edges) between nodes (u) and (v), then the diameter is given by:
[ \text{diameter}(T) = \max_{u,v \in T} d(u,v) ]
Your task is to compute the diameter of the tree. The input provides the number of vertices followed by the list of edges. The tree is guaranteed to have at least 2 vertices and at most 100 vertices.
inputFormat
The input is given via standard input (stdin). The first number is an integer (N) representing the number of vertices. It is followed by (N-1) pairs of integers where each pair (u\ v) indicates that there is an edge between vertices (u) and (v).
outputFormat
Output a single integer to standard output (stdout) representing the diameter of the tree.## sample
5
1 2
1 3
3 4
3 5
3