#P10062. [SNOI2024] 拉丁方
[SNOI2024] 拉丁方
Problem Description
We define an matrix to be a Latin square if and only if each row and each column is a permutation of .
Now you are given an submatrix in the top-left corner of matrix , i.e., the entries (, ). 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 , the number of test cases.
For each test case, the first line contains three integers , representing the matrix size and the size of the known submatrix.
Then follow lines, each containing numbers. The -th number in the -th line represents .
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 more lines, each containing 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 , 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 .
[Sample #3]
See the attachments latin/latin3.in and latin/latin3.ans. This sample satisfies the constraint limits of test points .
[Constraints]
For all testdata, it is guaranteed that , , , . It is guaranteed that in the input submatrix, no row or column contains two identical numbers.
Details are as follows:
| Test Point ID | Special Property | |
|---|---|---|
| None | ||
| A | ||
| B | ||
| C | ||
| None | ||
Special property A: It is guaranteed that .
Special property B: It is guaranteed that .
Special property C: It is guaranteed that .
Translated by ChatGPT 5