Arrays
Time limit4sMemory limit128 MB
Given two n by m arrays of distinct integers, decide whether one can be turned into the other by permuting rows and permuting columns.
- Level
Medium6 of 10
- Topics
- Hash map, Sorting, Implementation
- Solved
- No attempts yet
Problem
You are given an array of size filled with pairwise distinct integers. The following two operations may be applied to the array:
- interchange two rows,
- interchange two columns.
Two arrays are called alike if one of them can be obtained from the other by a sequence of the operations above.
Given several pairs of arrays, write a program that determines, for each pair, whether the two arrays are alike.
Input
The first line contains one integer (), the number of array pairs. Descriptions of the pairs follow.
Each description begins with a line containing two integers and (), separated by a single space, the common number of rows and columns of the two arrays.
The next lines describe the first array; the -th of them contains integers (), separated by single spaces, the values in the -th row of the first array.
The following lines describe the second array in the same way; the integers in its -th line are denoted .
All numbers within a single array are pairwise distinct.
Output
Print lines, one per pair. The -th line should contain TAK if the two arrays of the -th pair are alike, or NIE otherwise. (TAK and NIE mean "yes" and "no", respectively.)
Note
Interchanging rows or columns preserves the relations "lies in the same row" and "lies in the same column". Hence, for two arrays to be alike, they must first contain the same set of values, and any values sharing a row (or a column) in one array must also share a row (or a column) in the other. Checking whether the row-to-row and column-to-column correspondences can be fixed without contradiction settles whether the arrays are alike.