data structures math matrices

Please click on ads to support us..

Python Code:

import sys
import heapq
from math import ceil, inf, gcd
from collections import defaultdict , Counter, deque
from bisect import bisect_left , bisect_right
import sys, threading
import random

mod = pow(10, 9) + 7

def ip():
  return int(input())

def input():
  return sys.stdin.readline().strip()

def readList():
  return list(map(int,input().split()))

def solve():
    n , m = readList()
    a = []
    b = []
    
    for _ in range(n):
        a.append(readList())
    
    for _ in range(n):
        b.append(readList())
    
    rows = defaultdict(int)
    columns = defaultdict(int)
    
    for i in range(n):
        for j in range(m):
            rows[b[i][j]] = i
            columns[b[i][j]] = j
    
    for i in range(n):
        val1 = rows[a[i][0]]
        for j in range(m):
            if rows[a[i][j]] != val1:
                print("NO")
                return
        
    
    for i in range(m):
        val1 = columns[a[0][i]]
        for j in range(n):
            if columns[a[j][i]] != val1:
                print("NO")
                return
    
    print("YES")
            
    
T = int(input())
for _ in range(T):
    solve()


Comments

Submit
0 Comments
More Questions

1684C - Column Swapping
57C - Array
1713D - Tournament Countdown
33A - What is for dinner
810A - Straight A
1433C - Dominant Piranha
633A - Ebony and Ivory
1196A - Three Piles of Candies
299A - Ksusha and Array
448B - Suffix Structures
1092B - Teams Forming
1166C - A Tale of Two Lands
544B - Sea and Islands
152B - Steps
1174D - Ehab and the Expected XOR Problem
1511A - Review Site
1316A - Grade Allocation
838A - Binary Blocks
1515D - Phoenix and Socks
1624D - Palindromes Coloring
1552F - Telepanting
1692G - 2Sort
1191A - Tokitsukaze and Enhancement
903A - Hungry Student Problem
52B - Right Triangles
1712A - Wonderful Permutation
1712D - Empty Graph
1712B - Woeful Permutation
1712C - Sort Zero
1028B - Unnatural Conditions