P1275 Magic Board

Description

There is a kind of magic board: it is a rectangular panel divided into $n$ rows and $m$ columns, forming $n \times m$ cells. Each cell contains a small light bulb, and each bulb has two possible states (on or off). We can transform the board from one state to another using several operations. There are two types of operations: 1. Choose any row and toggle the state of all bulbs in that row, i.e., on becomes off and off becomes on. 2. Choose any two columns and swap their positions. Of course, not every pair of states can be transformed into each other through these operations. Your task is to determine whether two given board states can be transformed into each other.

Input Format

The file contains multiple datasets. The first line contains an integer $k$ ($0 < k \leq 5$), indicating there are $k$ datasets. For each dataset, the first line contains two integers $n$ and $m$ ($0 < n, m \leq 100$). The next $n$ lines describe the first magic board. Each line contains $m$ numbers ($0$ or $1$), separated by spaces. If the $y$-th number in the $x$-th row is $0$, then the bulb at row $x$, column $y$ is "on"; otherwise it is "off". The following $n$ lines describe the second magic board in the same format. There are no blank lines between any two datasets.

Output Format

Output $k$ lines, each describing the result for one dataset in order. If the two boards can be transformed into each other, output $\texttt{YES}$; otherwise, output $\texttt{NO}$. (Note: please use uppercase letters.)

Explanation/Hint

Translated by ChatGPT 5