C. Link Cut Centroids SOLUTIONS CODE FORCES

Link Cut Centroids SOLUTION

Fishing Prince cherishes trees, and he particularly adores trees with just a single centroid. The tree is an associated diagram without cycles. 
 
A vertex is a centroid of a tree just when you cut this vertex (eliminate it and eliminate all edges from this vertex), the size of the biggest associated part of the rest of the diagram is the littlest conceivable. 
 
For instance, the centroid of the accompanying tree is 2, since when you cut it, the size of the biggest associated part of the rest of the chart is 2 and it can’t be littler. 
 
Be that as it may, in certain trees, there may be more than one centroid, for instance: 
 
Both vertex 1 and vertex 2 are centroids on the grounds that the size of the biggest associated segment is 3 in the wake of cutting every one of them. 
 
Presently Fishing Prince has a tree. He should cut one edge of the tree (it intends to eliminate the edge). From that point onward, he should include one edge. The subsequent chart after these two tasks ought to be a tree. He can include the edge that he cut. 
 
He needs the centroid of the subsequent tree to be extraordinary. Help him and locate any conceivable method to make the activities. It tends to be demonstrated, that in any event one such way consistently exists. 
 
Information 
 
The information comprises of various experiments. The main line contains a number t (1≤t≤104) — the quantity of experiments. The depiction of the experiments follows. 
 
The principal line of each experiment contains a whole number n (3≤n≤105) — the quantity of vertices. 
 
Each of the following n−1 lines contains two whole numbers x,y (1≤x,y≤n). That is to say, that there exists an edge interfacing vertices x and y. 
 
It’s ensured that the given chart is a tree. 
 
It’s ensured that the total of n for all experiments doesn’t surpass 105. 
 
Yield 
 
For each experiment, print two lines. 
 
In the principal line print two whole numbers x1,y1 (1≤x1,y1≤n), which implies you cut the edge between vertices x1 and y1. There should exist edge interfacing vertices x1 and y1. 
 
In the subsequent line print two whole numbers x2,y2 (1≤x2,y2≤n), which implies you include the edge between vertices x2 and y2. 
 
The chart after these two tasks ought to be a tree. 
 
In the event that there are different arrangements you can print any. 
 
Model 
 
inputCopy 
 
 
 
1 2 
 
1 3 
 
2 4 
 
2 5 
 
 
1 2 
 
1 3 
 
1 4 
 
2 5 
 
2 6 
 
outputCopy 
 
1 2 
 
1 2 
 
1 3 
 
2 3 
 
Note 
 
Note that you can include a similar edge that you cut. 
 
In the principal experiment, subsequent to cutting and including a similar edge, the vertex 2 is as yet the main centroid. 
 
In the subsequent experiment, the vertex 2 turns into the main centroid in the wake of cutting the edge between vertices 1 and 3 and including the edge between vertices 2 and 3.
 

Leave a Comment