GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

Monday, 8 August 2022

[Solution] Cross Swapping Codeforces Solution




E. Cross Swapping
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 (1in), 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.

Solution Click Below:-  👉CLICK HERE👈

👇👇👇👇👇

 occupied

 For two matrices A and B of size n×n, let a(i1)n+j=Ai,j and b(i1)n+j=Bi,j. Then, the matrix A is lexicographically smaller than the matrix B when there exists an index i (1in2) such that ai<bi and for all indices j such that 1j<iaj=bj.

Input

The first line contains a single integer t (1t105) — the number of test cases.

The first line of each test case contains a single integer n (1n1000) — the size of the matrix.

The i-th line of the next n lines contains n integers Ai,1,Ai,2,,Ai,n (1Ai,j109) — description of the matrix A.

It is guaranteed that the sum of n2 over all test cases does not exceed 106.

Output

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


No comments:

Post a Comment