Refact.ai Match 1 (Codeforces Round 985)
D
Cool Graph
Codeforces
1900
原题
← CE →
翻译中…

You are given an undirected graph with $n$ vertices and $m$ edges.

翻译中…

You can perform the following operation at most $2\cdot \max(n,m)$ times:

翻译中…
  • Choose three distinct vertices $a$, $b$, and $c$, then for each of the edges $(a,b)$, $(b,c)$, and $(c,a)$, do the following:
  • If the edge does not exist, add it. On the contrary, if it exists, remove it.
翻译中…

A graph is called cool if and only if one of the following holds:

翻译中…
  • The graph has no edges, or
  • The graph is a tree.
翻译中…

You have to make the graph cool by performing the above operations. Note that you can use at most $2\cdot \max(n,m)$ operations.

翻译中…

It can be shown that there always exists at least one solution.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1\le t\le 10^4$) — the number of test cases. The description of test cases follows.

翻译中…

The first line of each test case contains two integers $n$ and $m$ ($3\le n\le 10^5$, $0\le m\le \min\left(\frac{n(n-1)}{2},2\cdot 10^5\right)$) — the number of vertices and the number of edges.

翻译中…

Then $m$ lines follow, the $i$-th line contains two integers $u_i$ and $v_i$ ($1\le u_i,v_i\le n$) — the two nodes that the $i$-th edge connects.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$, and the sum of $m$ over all test cases does not exceed $2\cdot 10^5$.

翻译中…

It is guaranteed that there are no self-loops or multiple-edges in the given graph.

翻译中…

Output

翻译中…

For each test case, in the first line output an integer $k$ ($0\le k\le 2\cdot \max(n, m)$) — the number of operations.

翻译中…

Then output $k$ lines, the $i$-th line containing three distinct integers $a$, $b$, and $c$ ($1\le a,b,c\le n$) — the three integers you choose in the $i$-th operation.

翻译中…

If there are multiple solutions, you can output any of them.

翻译中…

Note

翻译中…

In the first test case, the graph is already cool because there are no edges.

翻译中…

In the second test case, after performing the only operation, the graph becomes a tree, so it is cool.

翻译中…

In the third test case, the graph is already cool because it is a tree.

翻译中…

In the fourth test case, after performing the only operation, the graph has no edges, so it is cool.

翻译中…

In the fifth test case:

翻译中…

OperationGraph before the operationGraph after the operation$1$imageimage$2$imageimage$3$imageimage

翻译中…

Note that after the first operation, the graph has already become cool, and there are two extra operations. As the graph is still cool after the two extra operations, this is a valid answer.

翻译中…

样例

Input #1
5
3 0
3 1
1 2
3 2
1 2
2 3
3 3
1 2
2 3
3 1
6 6
1 2
1 6
4 5
3 4
4 6
3 6
Output #1
0
1
1 2 3
0
1
1 2 3
3
1 3 6
2 4 5
3 4 6
InputCopy OutputCopy
题目信息
题号
2029D
时间限制
3 s
内存限制
512 MB
标签
构造
数据结构
DFS 及类似
并查集
图论
贪心
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。