CF1250N.Wires

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

波利卡普斯拥有一台复杂的电子设备。该设备的核心是一块电路板。这块电路板上有 10910^9 个触点,编号从 11 到 10910^9。此外,还有 nn 根导线,编号从 11 到 nn,每根导线连接电路板上两个不同的触点。电信号可以在导线 AA 和导线 BB 之间传输,当且仅当满足以下任一条件:

  • 两根导线共享同一个触点;
  • 存在一条以导线 AA 为起点、以导线 BB 为终点的导线序列,且该序列中任意相邻的两根导线均共享一个触点。


图中展示了一块含 55 根导线的电路板。使用的触点编号为 2,5,7,8,10,132, 5, 7, 8, 10, 13。在此图中,电信号可以从导线 22 传输到导线 33,但无法传输到导线 11。

目前该电路板已损坏。波利卡普斯认为,若能重新焊接导线,使得任意两根导线之间均可传输电信号,则电路板即可修复。

波利卡普斯重焊一根导线的一端需耗时 11 分钟。即:将某根导线的两个触点之一更换为另一个触点,耗时 11 分钟。任何位于区间 [1,109][1, 10^9] 内的触点均可作为新触点使用。一根导线的两端必须始终焊接在两个不同的触点上。一根导线的两端均可被重焊,但这需要两次独立操作,总共耗时 22 分钟。

请计算波利卡普斯为使任意两根导线之间均可传输电信号所需重焊导线的最短时间,并输出一组最优的重焊操作序列。

输入格式

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 (1≤n≤1051 \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 (1≤xi,yi≤1091 \le x_i, y_i \le 10^9, xi≠yix_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.

输入包含一个或多个测试用例。第一行输入为一个整数 tt,表示测试用例的数量。随后是 tt 个测试用例。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5),表示导线的数量。接下来的 nn 行描述各条导线,每行包含两个以空格分隔的整数 xi,yix_i, y_i(1≤xi,yi≤1091 \le x_i, y_i \le 10^9,且 xi≠yix_i \neq y_i),表示第 ii 条导线所连接的两个触点。一对触点之间可能有多条导线相连。

所有测试用例中 nn 的总和不超过 10510^5。

输出格式

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 (1≤wj≤n1 \le w_j \le n, 1≤aj,bj≤1091 \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.

对于每个测试用例,首先输出一行,包含一个整数 kk —— 使得信号能在任意两根导线之间传输所需的最少重焊分钟数。接下来的 kk 行描述每次重焊操作。每次重焊操作由三个整数 wj,aj,bjw_j, a_j, b_j(1≤wj≤n1 \le w_j \le n, 1≤aj,bj≤1091 \le a_j, b_j \le 10^9)表示。该三元组表示:在第 jj 次重焊操作中,第 wjw_j 根导线原先焊接到接触点 aja_j 的一端,被重新焊接到接触点 bjb_j。每次重焊后,该导线必须连接两个不同的接触点。若存在多种最优重焊方案,输出任意一种即可。

输入输出样例

  • 输入#1

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

    输出#1

    0
    1
    2 3 5

输入解题思路,AI测评打分。不知道怎么写?

首页