Mole
Time limit1sMemory limit128 MB
Given a tree, remove one edge and add one edge (keeping it connected) to minimize the tree's diameter, and output the resulting diameter with the edges swapped.
Problem
A mole's home is a tree with N rooms and N-1 tunnels. Between any two rooms there is exactly one path, and the distance between two rooms is the number of tunnels on that path.
The mole will close one tunnel and dig one new tunnel. After the reconstruction, all rooms must still be connected. Among all possible reconstructions, minimize the greatest distance between any two rooms.
Given the current tunnels, print the minimum possible greatest distance after reconstruction, the tunnel to close, and the new tunnel to build.
Input
The first line contains the number of rooms N. The rooms are numbered from 1 to N. (3 ≤ N ≤ 300,000)
Each of the next N-1 lines contains two room numbers connected by a tunnel.
Output
On the first line, print the minimum possible greatest distance between two rooms after reconstruction.
On the second line, print the two room numbers connected by the tunnel to close.
On the third line, print the two room numbers connected by the new tunnel.
If there is more than one valid answer, you may print any one of them.