You are given an n×n chessboard. Rows and columns of the board are numbered from 0 to ‘n’-1. Cell (r,c) lies at the intersection of row number 'r' and column number ‘c’.
You are given the position of the ‘m’ rooks that are already placed on the chessboard. Your task is to find the maximum number of additional rooks with their positions that can be placed on the given chessboard such that no rook attacks some other rook. Print the positions (cells) of these additional rooks in lexicographical order. If there are multiple ways of placing the maximum number of additional rooks, you should find one that gives the lexicographically smallest sequence of cells after arranging their cells in lexicographical order.
Note :In lexicographical order, cell (a, b) comes before cell (c, d), if either a < c or (a = c and b < d).
The first line of input contains an 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 ‘m’ representing dimensions of the chessboard and the number of rooks that are already present in the chessboard respectively.
Then ‘m’ line follows, each containing 2 space-separated integers ‘r’, ‘c’ representing that there is a rook at cell (r, c).
Output Format :
For each test case, print in one line an integer ‘k’ representing the maximum number of additional rooks that can be placed on the chessboard and then print ‘k’ lines each of which contains 2 space-separated integers ‘r’ and ‘c’ representing that a rook is placed at cell (r, c). You need to print these cells in lexicographical order.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= n <= 10^4
0 <= m <= n
0 <= r, c < n
Where ‘T’ is the number of test cases and ‘n’ is the dimensions of the chessboard, ‘m’ is the number of rooks that are already present on the chessboard, and (r, c) represents the cell of the chessboard.
Time Limit: 1 sec
2
4 2
0 3
1 1
2 2
0 0
1 1
2
2 0
3 2
0
Test case 1 :
There is 4*4 chessboard with 2 rooks placed at cell (0, 3) and (1, 1).
Here you can place at most two additional rooks. If you try to place more than two additional rooks then there definitely exist an attacking pair of rooks.
There are two possible ways to place these additional 2 rooks, The lexicographical order of cells of these possible ways are following-:
1. (2, 0), (3, 2)
2. (2, 2), (3, 0)
(2, 0), (3, 2) is the lexicographically smallest of them. So it will be the answer.
Test case 2 :
You cannot place any additional rooks here. No matter where you place the rook it will attack at least one other rook.
2
1 0
3 2
0 1
1 2
1
0 0
1
2 0
Test case 1 :
There is only one cell in the chessboard, so you can place one rook at that cell.
Test case 2 :
You can place one additional rook at cell (2, 0). If you try to place a rook at any other position then there definitely exist an attacking pair of rooks.
One by one try to place a rook at each empty cell.
O(n^3), where ‘n’ is the dimensions of the chessboard.
There are three nested loops: the outermost loop, the innermost loop, and the middle loop each run for ‘n’ times.
O(n^2), where ‘n’ is the dimensions of the chessboard.
Space used by the boolean matrix is of order n^2.