#P10794. Maximum Tree Diameter After Operations
Maximum Tree Diameter After Operations
Maximum Tree Diameter After Operations
Given a tree with \(n\) nodes (numbered from \(1\) to \(n\)) and root \(1\), you are allowed to perform an arbitrary number of Station C Operations. In each operation, you select a leaf node \(x\) (a node other than the root whose degree is \(1\)), then choose an edge on the unique path from the root \(1\) to \(x\). You delete the chosen edge and add a new edge connecting \(x\) directly to the root.
Your task is to determine the maximum possible diameter of the tree after performing any sequence of such operations.
Note: The definition of a leaf node is a non-root node with degree \(1\).
inputFormat
The first line contains an integer \(n\) (\(2 \le n \le 10^5\)), 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 undirected edge between nodes \(u\) and \(v\). The tree is rooted at node \(1\).
outputFormat
Output a single integer — the maximum possible diameter of the tree after performing any sequence of operations.
sample
4
1 2
2 3
3 4
3