P15629 [CodeChef] RKS
提高-
题目描述

Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well.

Arya has a chessboard with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$); a square in row $r$ and column $c$ is denoted by $(r, c)$.

Arya has already placed $K$ rooks on the chessboard in such a way that no two rooks attack each other. Note that two rooks attack each other if they are in the same row or in the same column and there is no rook between them. She is busy learning how to tame a dragon right now, so she asked you to place as many other rooks as possible on the chessboard in such a way that afterwards, no two rooks on the chessboard attack each other. Help Arya and choose the positions of these rooks.

------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $K$.
Each of the next $K$ lines contains two space-separated integers $r$ and $c$ denoting that Arya placed a rook on the square $(r, c)$.

------ Output ------
For each test case:
Let's assume that you placed $P$ rooks on squares $(r_{1}, c_{1}), (r_{2}, c_{2}), \ldots, (r_{P}, c_{P})$.
You should print a single line containing $2P+1$ space-separated integers $P, r_{1}, c_{1}, \ldots, r_{P}, c_{P}$.
$P$ must be maximum possible.
If there are multiple possible answers, the sequence $r_{1}, c_{1}, \ldots, r_{P}, c_{P}$ should be lexicographically smallest.

------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 10^{6}$
$0 ≤ K ≤ N$
$1 ≤ r_{i}, c_{i} ≤ N$ for each valid $i$
no two initially placed rooks attack each other
the positions of all initially placed rooks are pairwise distinct
the sum of $N$ over all test cases does not exceed $10^{6}$

------ Subtasks ------
Subtask #1 (10 points):
$T = 10$
$N ≤ 8$

Subtask #2 (20 points):
$T = 10$
$N ≤ 1,000$

Subtask #3 (70 points): original constraints

----- Sample Input 1 ------
2
4 2
1 4
2 2
4 0
----- Sample Output 1 ------
2 3 1 4 3
4 1 1 2 2 3 3 4 4

输入格式

见题面

输出格式

见题面

样例输入
2 4 2 1 4 2 2 4 0
样例输出
2 3 1 4 3 4 1 1 2 2 3 3 4 4