

Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 点
問題文
から の番号がついた 台のサーバーと から の番号がついた 本のケーブルがあります。
ケーブル はサーバー とサーバー を双方向に結んでいます。
以下の操作を何回か( 回でもよい)行うことで、全てのサーバー同士がケーブルを介して繋がっているようにしてください。
- 操作:ケーブルを つ選び、その片方の端を別のサーバーに繋ぎ変える
操作の最小回数と、最小回数となる操作列を求めてください。
制約
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
出力
操作回数の最小値を とする。 行出力せよ。
行目には を出力せよ。
行目には 回目の操作で選ぶケーブルの番号、繋ぎ変える前に繋がっていたサーバーの番号、繋ぎ変えたあと繋がっているサーバーの番号をこの順に空白区切りで出力せよ。
条件を満たす解が複数存在する場合、どれを出力しても正解とみなされる。
入力例 1Copy
4 5 1 1 1 2 2 1 3 4 4 4
出力例 1Copy
1 1 1 3
ケーブル のサーバー に繋がっている側の端をサーバー に繋ぎ変えることで、サーバー同士がケーブルを介して繋がっている状態にすることができます。
このほか、「ケーブル のサーバー に繋がっている側の端をサーバー に繋ぎ変える」という操作や、「ケーブル のサーバー に繋がっている側の端をサーバー に繋ぎ変える」という操作でもサーバー同士がケーブルを介して繋がっている状態にすることができるため、正解とみなされます。
入力例 2Copy
4 3 3 4 4 1 1 2
出力例 2Copy
0
操作をする必要がないこともあります。
入力例 3Copy
5 4 3 3 3 3 3 3 3 3
出力例 3Copy
4 1 3 5 2 3 4 3 3 2 4 3 1
Score : points
Problem Statement
There are servers numbered from to and cables numbered from to .
Cable connects servers and 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
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Let the minimum number of operations be . Print lines.
- The first line should contain .
- The -th line should contain three space-separated integers: the number of the cable chosen in the -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
4 5 1 1 1 2 2 1 3 4 4 4
Sample Output 1Copy
1 1 1 3
By reconnecting the end of cable that is connected to server to server , the servers can be connected via cables.
Operations such as reconnecting the end of cable that is connected to server to server , or reconnecting the end of cable that is connected to server to server , will also result in all servers being connected and are considered correct.
Sample Input 2Copy
4 3 3 4 4 1 1 2
Sample Output 2Copy
0
No operation may be necessary.
Sample Input 3Copy
5 4 3 3 3 3 3 3 3 3
Sample Output 3Copy
4 1 3 5 2 3 4 3 3 2 4 3 1