E - Cables and Servers Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 450450

問題文

11 から NN の番号がついた NN 台のサーバーと 11 から MM の番号がついた MM 本のケーブルがあります。
ケーブル ii はサーバー AiA_i とサーバー BiB_i を双方向に結んでいます。

以下の操作を何回か( 00 回でもよい)行うことで、全てのサーバー同士がケーブルを介して繋がっているようにしてください。

  • 操作:ケーブルを 11 つ選び、その片方の端を別のサーバーに繋ぎ変える

操作の最小回数と、最小回数となる操作列を求めてください。

制約

  • 2N2×1052 \leq N \leq 2\times 10^5
  • N1M2×105N-1 \leq M \leq 2\times 10^5
  • 1Ai,BiN1 \leq A_i,B_i \leq N
  • 入力は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

NN MM
A1A_1 B1B_1
A2A_2 B2B_2
\vdots
AMA_M BMB_M

出力

操作回数の最小値を KK とする。K+1K+1 行出力せよ。
11 行目には KK を出力せよ。
i+1i+1 行目には ii 回目の操作で選ぶケーブルの番号、繋ぎ変える前に繋がっていたサーバーの番号、繋ぎ変えたあと繋がっているサーバーの番号をこの順に空白区切りで出力せよ。

条件を満たす解が複数存在する場合、どれを出力しても正解とみなされる。


入力例 1Copy

Copy
4 5
1 1
1 2
2 1
3 4
4 4

出力例 1Copy

Copy
1
1 1 3

ケーブル 11 のサーバー 11 に繋がっている側の端をサーバー 33 に繋ぎ変えることで、サーバー同士がケーブルを介して繋がっている状態にすることができます。

図

このほか、「ケーブル 55 のサーバー 44 に繋がっている側の端をサーバー 11 に繋ぎ変える」という操作や、「ケーブル 22 のサーバー 22 に繋がっている側の端をサーバー 33 に繋ぎ変える」という操作でもサーバー同士がケーブルを介して繋がっている状態にすることができるため、正解とみなされます。


入力例 2Copy

Copy
4 3
3 4
4 1
1 2

出力例 2Copy

Copy
0

操作をする必要がないこともあります。


入力例 3Copy

Copy
5 4
3 3
3 3
3 3
3 3

出力例 3Copy

Copy
4
1 3 5
2 3 4
3 3 2
4 3 1

Score : 450450 points

Problem Statement

There are NN servers numbered from 11 to NN and MM cables numbered from 11 to MM.
Cable ii connects servers AiA_i and BiB_i bidirectionally.

By performing the following operation some number of times (possibly zero), make all servers connected via cables.

  • Operation: Choose one cable and reconnect one of its ends to a different server.

Find the minimum number of operations required and output an operation sequence achieving this minimum.

Constraints

  • 2N2×1052 \leq N \leq 2\times 10^5
  • N1M2×105N-1 \leq M \leq 2\times 10^5
  • 1Ai,BiN1 \leq A_i, B_i \leq N
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN MM
A1A_1 B1B_1
A2A_2 B2B_2
\vdots
AMA_M BMB_M

Output

Let the minimum number of operations be KK. Print K+1K+1 lines.

  • The first line should contain KK.
  • The (i+1)(i+1)-th line should contain three space-separated integers: the number of the cable chosen in the ii-th operation, the server number that was originally connected at that end, and the server number to which it is connected after the operation, in this order.

If there are multiple valid solutions, any one of them will be accepted.


Sample Input 1Copy

Copy
4 5
1 1
1 2
2 1
3 4
4 4

Sample Output 1Copy

Copy
1
1 1 3

By reconnecting the end of cable 11 that is connected to server 11 to server 33, the servers can be connected via cables.

図

Operations such as reconnecting the end of cable 55 that is connected to server 44 to server 11, or reconnecting the end of cable 22 that is connected to server 22 to server 33, will also result in all servers being connected and are considered correct.


Sample Input 2Copy

Copy
4 3
3 4
4 1
1 2

Sample Output 2Copy

Copy
0

No operation may be necessary.


Sample Input 3Copy

Copy
5 4
3 3
3 3
3 3
3 3

Sample Output 3Copy

Copy
4
1 3 5
2 3 4
3 3 2
4 3 1


2025-04-20 (Sun)
23:56:34 +00:00