#P6431. [COCI 2008/2009 #1] KRTICA

[COCI 2008/2009 #1] KRTICA

Description

There is a tree with nn nodes, and all edge weights are 11.

Now you want to delete one edge and add one edge, so that the distance between the two farthest nodes is as small as possible.

Input Format

The first line contains an integer nn.

The next n1n-1 lines each contain two integers aa and bb, indicating that there is an undirected edge from aa to bb in the tree.

Output Format

This problem uses an SPJ.

The first line contains a single integer, which is the distance between the two farthest nodes after deleting one edge and adding one edge.

The second line contains two integers, indicating the edge that is deleted.

The third line contains two integers, indicating the edge that is added.

4
1 2
2 3
3 4 
2
3 4
4 2 

7
1 3
2 3
2 7
4 3
7 5
3 6 

3
2 3
7 3 

Hint

Constraints

  • For 40%40\% of the testdata, n30n \le 30 is guaranteed.
  • For 70%70\% of the testdata, n3×103n \le 3 \times 10^3 is guaranteed.
  • For 100%100\% of the testdata, 1n3×1051 \le n \le 3 \times 10^5, and 1a,bn1 \le a, b \le n.

Scoring

  • If the first line of the output is incorrect, you get 00 points.
  • If the first line of the output is correct, but the remaining numbers are incorrect or fewer than four numbers are printed in total, you get 70%70\% of the score for the corresponding test point.
  • If the first line of the output is correct, and the given solution is feasible and correct, you get 100%100\% of the score for the corresponding test point.

Notes

This problem is translated from T6 KRTICA of Croatian Open Competition in Informatics 2008/2009, Contest #1.

SPJ provided by @Tweetuzki.

Translated by ChatGPT 5