#arc176a. A - 01 Matrix Again

A - 01 Matrix Again

Score: 400400 points

问题陈述

有一个 N×NN \times N 的网格。用 (i,j)(i, j) 表示从顶部数第 ii 行,从左边数第 jj 列的单元格。

你需要用 0011 填充每个单元格。构建一种填充网格的方法,以满足以下所有条件:

  • 单元格 (A1,B1),(A2,B2),,(AM,BM)(A_1,B_1), (A_2,B_2), \dots, (A_M,B_M) 包含 11
  • ii 行的整数之和为 MM(1iN)(1 \le i \le N)
  • ii 列的整数之和为 MM(1iN)(1 \le i \le N)

可以证明,在这个问题的约束条件下,至少有一种填充网格的方法可以满足这些条件。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

There is an N×NN \times N grid. Let (i,j)(i, j) denote the cell at the ii-th row from the top and the jj-th column from the left.

You are to fill each cell with 00 or 11. Construct one method to fill the grid that satisfies all of the following conditions:

  • The cells (A1,B1),(A2,B2),,(AM,BM)(A_1,B_1), (A_2,B_2), \dots, (A_M,B_M) contain 11.
  • The integers in the ii-th row sum to MM. (1iN)(1 \le i \le N)
  • The integers in the ii-th column sum to MM. (1iN)(1 \le i \le N)

It can be proved that under the constraints of this problem, there is at least one method to fill the grid that satisfies the conditions.

Constraints

  • 1N1051 \le N \le 10^5
  • 1Mmin(N,10)1 \le M \le \min(N,10)
  • 1Ai,BiN1 \le A_i, B_i \le N
  • (Ai,Bi)(Aj,Bj)(A_i, B_i) \neq (A_j, B_j) if iji \neq j.

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 (x1,y1),(x2,y2),,(xk,yk)(x_1,y_1), (x_2,y_2), \dots, (x_k,y_k) be the cells that contain 11, and print the following:

kk

x1x_1 y1y_1

x2x_2 y2y_2

\vdots

xkx_k yky_k

If multiple methods satisfy the conditions, any of them will be considered correct.

Sample Input 1

4 2
1 4
3 2

Sample Output 1

8
1 2
1 4
2 1
2 4
3 2
3 3
4 1
4 3

This output fills the grid as follows. All the conditions are satisfied, so this output is correct.

0101
1001
0110
1010

Sample Input 2

3 3
3 1
2 3
1 3

Sample Output 2

9
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

Sample Input 3

7 3
1 7
7 6
6 1

Sample Output 3

21
1 6
2 4
4 1
7 3
3 6
4 5
6 1
1 7
7 6
3 5
2 2
6 3
6 7
5 4
5 2
2 5
5 3
1 4
7 1
4 7
3 2