#K44732. Maximizing Tree Depth
Maximizing Tree Depth
Maximizing Tree Depth
You are given an undirected tree with n nodes and n-1 edges. Each node is labeled with a distinct integer from 1 to n, and the depth of a node is defined as the number of edges from the node to the root (node 1). You are allowed to perform exactly one operation in which you select two nodes u and v (which are not directly connected) and add an edge between them, while removing one of the existing edges to ensure that the graph remains a tree.
Your task is to determine the maximum possible depth of the tree after such an operation. It can be shown that the answer to this problem is equivalent to finding the diameter of the tree, which is mathematically defined as:
$\displaystyle \max_{u,v}\; d(u,v)$
where d(u,v) represents the distance (the number of edges) between nodes u and v.
inputFormat
The first line contains a single integer n representing the number of nodes in the tree. Each of the next n-1 lines contains two space-separated integers u and v, indicating that there is an edge between node u and node v.
outputFormat
Output a single integer — the maximum possible depth (i.e., the diameter of the tree) after performing one valid operation.
## sample4
1 2
2 3
2 4
2
</p>