01 Matrix Again

AtCoder
IDarc176_a
Time4000ms
Memory256MB
Difficulty
There is an $N \times N$ grid. Let $(i, j)$ denote the cell at the $i$\-th row from the top and the $j$\-th column from the left. You are to fill each cell with $0$ or $1$. Construct one method to fill the grid that satisfies all of the following conditions: * The cells $(A_1,B_1), (A_2,B_2), \dots, (A_M,B_M)$ contain $1$. * The integers in the $i$\-th row sum to $M$. $(1 \le i \le N)$ * The integers in the $i$\-th column sum to $M$. $(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 * $1 \le N \le 10^5$ * $1 \le M \le \min(N,10)$ * $1 \le A_i, B_i \le N$ * $(A_i, B_i) \neq (A_j, B_j)$ if $i \neq j$. ## Input The input is given from Standard Input in the following format: $N$ $M$ $A_1$ $B_1$ $A_2$ $B_2$ $\vdots$ $A_{M}$ $B_{M}$ [samples]
Samples
Input #1
4 2
1 4
3 2
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
Input #2
3 3
3 1
2 3
1 3
Output #2
9
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Input #3
7 3
1 7
7 6
6 1
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
API Response (JSON)
{
  "problem": {
    "name": "01 Matrix Again",
    "description": {
      "content": "There is an $N \\times N$ grid. Let $(i, j)$ denote the cell at the $i$\\-th row from the top and the $j$\\-th column from the left. You are to fill each cell with $0$ or $1$. Construct one method to fil",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 4000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "arc176_a"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "There is an $N \\times N$ grid. Let $(i, j)$ denote the cell at the $i$\\-th row from the top and the $j$\\-th column from the left.\nYou are to fill each cell with $0$ or $1$. Construct one method to fil...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments