CF BUDDY
← Problems·

1980E · Permutation of Rows and Columns

1600 · constructive algorithms, data structures, greedy

Problem: You have been given a matrix aa of size nn by mm, containing a permutation of integers from 11 to nmn \cdot m.

A permutation of nn integers is an array containing all numbers from 11 to nn exactly once. For example, the arrays [1][1], [2,1,3][2, 1, 3], [5,4,3,2,1][5, 4, 3, 2, 1] are permutations, while the arrays [1,1][1, 1], [100][100], [1,2,4,5][1, 2, 4, 5] are not.

A matrix contains a permutation if, when all its elements are written out, the resulting array is a permutation. Matrices [[1,2],[3,4]][[1, 2], [3, 4]], [[1]][[1]], [[1,5,3],[2,6,4]][[1, 5, 3], [2, 6, 4]] contain permutations, while matrices [[2]][[2]], [[1,1],[2,2]][[1, 1], [2, 2]], [[1,2],[100,200]][[1, 2], [100, 200]] do not.

You can perform one of the following two actions in one operation:

  • choose columns cc and dd (1c,dm1 \le c, d \le m, cdc \ne d) and swap these columns;
  • choose rows cc and dd (1c,dn1 \le c, d \le n, cdc \ne d) and swap these rows.

You can perform any number of operations.

You are given the original matrix aa and the matrix bb. Your task is to determine whether it is possible to transform matrix aa into matrix bb using the given operations.

Input Format: The first line contains an integer tt (1t1041 \le t \le 10^4) — the number of test cases. The descriptions of the test cases follow.

The first line of each test case description contains 22 integers nn and mm (1n,mnm21051 \le n, m \le n \cdot m \le 2 \cdot 10^5) — the sizes of the matrix.

The next nn lines contain mm integers aija_{ij} each (1aijnm1 \le a_{ij} \le n \cdot m). It is guaranteed that matrix aa is a permutation.

The next nn lines contain mm integers bijb_{ij} each (1bijnm1 \le b_{ij} \le n \cdot m). It is guaranteed that matrix bb is a permutation.

It is guaranteed that the sum of the values nmn \cdot m for all test cases does not exceed 21052 \cdot 10^5.

Output Format: For each test case, output "YES" if the second matrix can be obtained from the first, and "NO" otherwise.

You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.

Note: In the second example, the original matrix looks like this:

(1234)\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}

By swapping rows 11 and 22, it becomes:

(3412)\begin{pmatrix} 3 & 4 \\ 1 & 2 \end{pmatrix}

By swapping columns 11 and 22, it becomes equal to matrix bb:

(4321)\begin{pmatrix} 4 & 3 \\ 2 & 1 \end{pmatrix}

Sample Cases

Case 1

Input

7
1 1
1
1
2 2
1 2
3 4
4 3
2 1
2 2
1 2
3 4
4 3
1 2
3 4
1 5 9 6
12 10 4 8
7 11 3 2
1 5 9 6
12 10 4 8
7 11 3 2
3 3
1 5 9
6 4 2
3 8 7
9 5 1
2 4 6
7 8 3
2 3
1 2 6
5 4 3
6 1 2
3 4 5
1 5
5 1 2 3 4
4 2 5 1 3

Output

YES
YES
NO
YES
YES
NO
YES

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback