atcoder#ARC160E. [ARC160E] Make Biconnected
[ARC160E] Make Biconnected
Score : points
Problem Statement
You are given an undirected tree with vertices. The degree of every vertex in G is at most 3. The vertices are numbered to . The edges are numbered to , and edge connects vertex and vertex . Each vertex has a fixed weight, and the weight of vertex is .
You will add zero or more edges to . The cost of adding an edge between vertex and vertex is .
Print one way to add edges to satisfy the following condition for the minimum possible total cost.
- is -vertex-connected. In other words, for every vertex in , removing and its incident edges from would not disconnect .
You have test cases to solve.
Constraints
- The given graph is a tree.
- The degree of every vertex in the given graph is at most .
- is an integer.
- The sum of across the test cases is at most .
Input
The input is given from Standard Input in the following format, where represents the -th test case:
Each test case is in the following format:
Output
For each test case, print a solution in the following format, where:
- is the number of added edges, and
- the -th added edge connects vertex and vertex .
If multiple solutions exist, any of them would be accepted.
2
3
2 3 5
1 2
2 3
7
1 10 100 1000 10000 100000 1000000
1 2
2 3
2 4
3 5
3 6
4 7
1
1 3
2
7 6
1 5
In the first test case, adding an edge connecting vertex and vertex makes satisfy the condition in the problem statement. The cost of this is . There is no way to add edges to satisfy the condition for a cost of less than , so this is a valid solution.
In the second test case, the solution above has a total cost of $(W_7 + W_6) + (W_1 + W_5) = 1100000 + 10001 = 1110001$, which is the minimum possible.