#C7977. Tree Diameter
Tree Diameter
Tree Diameter
You are given an undirected tree with N nodes. The tree is defined by N - 1 edges. The diameter of a tree is the length of the longest path between any two nodes. Formally, if we denote the distance between two nodes u and v as \(d(u, v)\), then the tree diameter is defined as:
\[ \max_{u,v \in V} d(u,v) \]In each test case, you are provided with a list of node values (which can be ignored) and the list of edges defining the tree. Your task is to compute the diameter of the tree. The path length is measured in the number of edges.
inputFormat
The input begins with an integer T denoting the number of test cases. For each test case:
- An integer N representing the number of nodes in the tree.
- A line containing N integers which represent node values (this data is not used to compute the answer).
- N - 1 lines follow, each containing two integers u and v which denote an undirected edge between nodes u and v.
All input is provided via standard input (stdin).
outputFormat
For each test case, output a single integer representing the diameter of the tree (i.e. the maximum number of edges in the longest path). Each result should be printed on a new line to standard output (stdout).
## sample2
5
1 2 3 4 5
1 2
1 3
3 4
3 5
3
1 2 3
1 2
1 3
3
2
</p>