QOJ.ac

QOJ

时间限制: 5 s 内存限制: 1024 MB 总分: 100 难度: [显示]

#7999. Latin Square

统计

We define an $n \times n$ matrix $A$ as a Latin square if and only if every row and every column is a permutation of $1 \sim n$.

You are given an $R \times C$ submatrix at the top-left corner of a matrix $A$, i.e., $A_{i,j}$ ($1 \le i \le R, 1 \le j \le C$). Determine whether it is possible to fill in the remaining positions such that the matrix becomes a Latin square.

Input

The input is read from the file latin.in.

There are multiple test cases. The first line contains an integer $T$, representing the number of test cases.

For each test case, the first line contains three integers $n, R, C$, representing the matrix size and the size of the known submatrix.

The next $R$ lines each contain $C$ integers, where the $j$-th number in the $i$-th line represents $A_{i,j}$.

Output

The output is written to the file latin.out.

For each test case, output "Yes" or "No" on the first line, indicating whether a valid Latin square can be found.

If a valid Latin square can be found, output $n$ lines, each containing $n$ numbers, representing a valid Latin square. If there are multiple valid solutions, any one of them is acceptable.

Examples

Input 1

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

Output 1

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

Note

In the first example, for the second test case, it can be observed from the first two rows that $A_{1,3} = A_{2,3} = 3$, so no valid Latin square exists.

For the third test case, the output is a valid Latin square, and its top-left corner matches the input matrix. Another valid solution is:

$$ \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} $$

Examples

Input 2

See the provided files latin/latin2.in and latin/latin2.ans. This example satisfies the constraints for test cases $6 \sim 7$.

Input 3

See the provided files latin/latin3.in and latin/latin3.ans. This example satisfies the constraints for test cases $11 \sim 12$.

Constraints

For all data, it is guaranteed that $1 \le T \le 10$, $1 \le n \le 500$, $1 \le R, C \le n$, and $1 \le A_{i,j} \le n$. It is also guaranteed that the input submatrix does not contain two identical numbers in any row or column.

The details are as follows:

Test Case ID $n \le$ Special Property
$1 \sim 2$ $6$ None
$3 \sim 4$ $10$ None
$5$ $500$ A
$6 \sim 7$ $100$ None
$8 \sim 9$ $300$ B
$10$ $500$ None
$11 \sim 12$ $500$ C
$13 \sim 14$ $100$ None
$15 \sim 16$ $300$ None
$17 \sim 20$ $500$ None

Special Property A: Guaranteed $R = 1$. Special Property B: Guaranteed $C = n$. Special Property C: Guaranteed $R, C \le \frac{n}{2}$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.