CF BUDDY
← Problems·

1250N · Wires

2000 · dfs and similar, graphs, greedy

Problem: Polycarpus has a complex electronic device. The core of this device is a circuit board. The board has 10910^9 contact points which are numbered from 11 to 10910^9. Also there are nn wires numbered from 11 to nn, each connecting two distinct contact points on the board. An electric signal can pass between wires AA and BB if:

  • either both wires share the same contact point;
  • or there is a sequence of wires starting with AA and ending with BB, and each pair of adjacent wires in the sequence share a contact point.

The picture shows a circuit board with 55 wires. Contact points with numbers 2,5,7,8,10,132, 5, 7, 8, 10, 13 are used. Here an electrical signal can pass from wire 22 to wire 33, but not to wire 11.

Currently the circuit board is broken. Polycarpus thinks that the board could be fixed if the wires were re-soldered so that a signal could pass between any pair of wires.

It takes 11 minute for Polycarpus to re-solder an end of a wire. I.e. it takes one minute to change one of the two contact points for a wire. Any contact point from range [1,109][1, 10^9] can be used as a new contact point. A wire's ends must always be soldered to distinct contact points. Both wire's ends can be re-solded, but that will require two actions and will take 22 minutes in total.

Find the minimum amount of time Polycarpus needs to re-solder wires so that a signal can pass between any pair of wires. Also output an optimal sequence of wire re-soldering.

Input Format: The input contains one or several test cases. The first input line contains a single integer tt — number of test cases. Then, tt test cases follow.

The first line of each test case contains a single integer nn (1n1051 \le n \le 10^5) — the number of wires. The following nn lines describe wires, each line containing two space-separated integers xi,yix_i, y_i (1xi,yi1091 \le x_i, y_i \le 10^9, xiyix_i \neq y_i) — contact points connected by the ii-th wire. A couple of contact points can be connected with more than one wire.

Sum of values of nn across all test cases does not exceed 10510^5.

Output Format: For each test case first print one line with a single integer kk — the minimum number of minutes needed to re-solder wires so that a signal can pass between any pair of wires. In the following kk lines print the description of re-solderings. Each re-soldering should be described by three integers wj,aj,bjw_j, a_j, b_j (1wjn1 \le w_j \le n, 1aj,bj1091 \le a_j, b_j \le 10^9). Such triple means that during the jj-th re-soldering an end of the wjw_j-th wire, which was soldered to contact point aja_j, becomes soldered to contact point bjb_j instead. After each re-soldering of a wire it must connect two distinct contact points. If there are multiple optimal re-solderings, print any of them.

Sample Cases

Case 1

Input

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

Output

0
1
2 3 5

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback