CF1713E.Cross Swapping

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a square matrix AA of size n×nn \times n whose elements are integers. We will denote the element on the intersection of the ii -th row and the jj -th column as Ai,jA_{i,j} .

You can perform operations on the matrix. In each operation, you can choose an integer kk , then for each index ii ( 1in1 \leq i \leq n ), swap Ai,kA_{i, k} with Ak,iA_{k, i} . Note that cell Ak,kA_{k, k} remains unchanged.

For example, for n=4n = 4 and k=3k = 3 , this matrix will be transformed like this:

The operation k=3k = 3 swaps the blue row with the green column.You can perform this operation any number of times. Find the lexicographically smallest matrix ^\dagger you can obtain after performing arbitrary number of operations.

{}^\dagger For two matrices AA and BB of size n×nn \times n , let a(i1)n+j=Ai,ja_{(i-1) \cdot n + j} = A_{i,j} and b(i1)n+j=Bi,jb_{(i-1) \cdot n + j} = B_{i,j} . Then, the matrix AA is lexicographically smaller than the matrix BB when there exists an index ii ( 1in21 \leq i \leq n^2 ) such that ai<bia_i < b_i and for all indices jj such that 1j<i1 \leq j < i , aj=bja_j = b_j .

输入格式

The first line contains a single integer tt ( 1t1051 \leq t \leq 10^5 ) — the number of test cases.

The first line of each test case contains a single integer nn ( 1n10001 \leq n \leq 1000 ) — the size of the matrix.

The ii -th line of the next nn lines contains nn integers Ai,1,Ai,2,,Ai,nA_{i, 1}, A_{i, 2}, \dots, A_{i, n} ( 1Ai,j1091 \le A_{i, j} \le 10^9 ) — description of the matrix AA .

It is guaranteed that the sum of n2n^2 over all test cases does not exceed 10610^6 .

输出格式

For each test case, print nn lines with nn integers each — the lexicographically smallest matrix.

输入输出样例

  • 输入#1

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

    输出#1

    2 1 1
    2 1 1
    2 2 2
    3 1 1 2
    3 1 2 1
    3 3 3 3
    2 3 2 1

说明/提示

Note that in every picture below the matrix is transformed in such a way that the blue rows are swapped with the green columns.

In the first test case, we can perform 11 operation for k=3k = 3 . The matrix will be transformed as below:

In the second test case, we can perform 22 operations for k=1k = 1 and k=3k = 3 :

首页