CF1713E.Cross Swapping
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a square matrix A of size n×n whose elements are integers. We will denote the element on the intersection of the i -th row and the j -th column as Ai,j .
You can perform operations on the matrix. In each operation, you can choose an integer k , then for each index i ( 1≤i≤n ), swap Ai,k with Ak,i . Note that cell Ak,k remains unchanged.
For example, for n=4 and k=3 , this matrix will be transformed like this:
The operation k=3 swaps the blue row with the green column.You can perform this operation any number of times. Find the lexicographically smallest matrix † you can obtain after performing arbitrary number of operations.
† For two matrices A and B of size n×n , let a(i−1)⋅n+j=Ai,j and b(i−1)⋅n+j=Bi,j . Then, the matrix A is lexicographically smaller than the matrix B when there exists an index i ( 1≤i≤n2 ) such that ai<bi and for all indices j such that 1≤j<i , aj=bj .
输入格式
The first line contains a single integer t ( 1≤t≤105 ) — the number of test cases.
The first line of each test case contains a single integer n ( 1≤n≤1000 ) — the size of the matrix.
The i -th line of the next n lines contains n integers Ai,1,Ai,2,…,Ai,n ( 1≤Ai,j≤109 ) — description of the matrix A .
It is guaranteed that the sum of n2 over all test cases does not exceed 106 .
输出格式
For each test case, print n lines with n 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 1 operation for k=3 . The matrix will be transformed as below:
In the second test case, we can perform 2 operations for k=1 and k=3 :