#P10062. [SNOI2024] 拉丁方

    ID: 11444 远端评测题 5000ms 1024MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>各省省选2024Special JudgeO2优化陕西

[SNOI2024] 拉丁方

Problem Description

We define an n×nn \times n matrix AA to be a Latin square if and only if each row and each column is a permutation of 1n1 \sim n.

Now you are given an R×CR \times C submatrix in the top-left corner of matrix AA, i.e., the entries Ai,jA_{i, j} (1iR1 \le i \le R, 1jC1 \le j \le C). Determine whether it is possible to fill the remaining entries so that the whole matrix becomes a Latin square.

Input Format

Multiple test cases. The first line contains an integer TT, the number of test cases.
For each test case, the first line contains three integers n,R,Cn, R, C, representing the matrix size and the size of the known submatrix.
Then follow RR lines, each containing CC numbers. The jj-th number in the ii-th line represents Ai,jA_{i, j}.

Output Format

For each test case, output a string Yes or No in the first line, indicating whether a Latin square satisfying the conditions can be found.
If it can be found, then output nn more lines, each containing nn numbers, representing one valid Latin square. If multiple valid solutions exist, output any one of them.

3
2 1 1
1
3 2 2
1 2
2 1
5 2 3
1 2 3
4 3 2

Yes
1 2
2 1
No
Yes
1 2 3 4 5
4 3 2 5 1
2 4 5 1 3
3 5 1 2 4
5 1 4 3 2

Hint

[Sample #1 Explanation]

In the first sample, for the second test case, from the first two rows we can see that A1,3=A2,3=3A_{1, 3} = A_{2, 3} = 3, so no Latin square satisfying the conditions exists.

For the third test case, we can see that the output is a valid Latin square, and its top-left corner matches the input matrix. The following is also a valid solution.

$$\begin{bmatrix} 1 & 2 & 3 & 5 & 4 \\ 4 & 3 & 2 & 1 & 5 \\ 3 & 5 & 1 & 4 & 2 \\ 2 & 4 & 5 & 3 & 1 \\ 5 & 1 & 4 & 2 & 3 \end{bmatrix}$$

[Sample #2]

See the attachments latin/latin2.in and latin/latin2.ans. This sample satisfies the constraint limits of test points 676 \sim 7.


[Sample #3]

See the attachments latin/latin3.in and latin/latin3.ans. This sample satisfies the constraint limits of test points 111211 \sim 12.


[Constraints]

For all testdata, it is guaranteed that 1T101 \le T \le 10, 1n5001 \le n \le 500, 1R,Cn1 \le R, C \le n, 1Ai,jn1 \le A_{i, j} \le n. It is guaranteed that in the input submatrix, no row or column contains two identical numbers.

Details are as follows:

Test Point ID nn \le Special Property
121 \sim 2 66 None
343 \sim 4 1010
55 500500 A
676 \sim 7 100100 B
898 \sim 9 300300
1010 500500
111211 \sim 12 C
131413 \sim 14 100100 None
151615 \sim 16 300300
172017 \sim 20 500500

Special property A: It is guaranteed that R=1R = 1.
Special property B: It is guaranteed that C=nC = n.
Special property C: It is guaranteed that R,Cn2R, C \le \frac{n}{2}.

Translated by ChatGPT 5