Codeforces Round 1086 (Div. 2)
A
Bingo Candies
Codeforces
800
原题
←B →
翻译中…

Alice has a magic board. The board is described as a $n\times n$ grid; each tile has a colored candy in it. The color of the candy in the $i$-th row, $j$-th column is $a_{i,j}$.

翻译中…

Bob wants to know if he can rearrange the board in some way so that no row or column consists of $n$ candies of the same color.

翻译中…

Your task is to determine whether such a rearrangement exists.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.

翻译中…

The first line of each test case contains an integer $n$ ($1\le n\le 100$), denoting the size of the board.

翻译中…

The following $n$ lines contain $n$ integers each; the $j$-th integer on the $i$-th line is $a_{i,j}$ ($1\le a_{i,j}\le n^2$), denoting the color of candies on the board.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $500$.

翻译中…

Output

翻译中…

For each test case, print "YES" if a valid rearrangement exists, and "NO" otherwise.

翻译中…

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

翻译中…

Note

翻译中…

In the first test case, no row or column consists of all candies with the same color; the board can be left as it is.

翻译中…

In the second test case, the first row consists of all candies of color $1$. The board can be rearranged by swapping $a_{1,1}$ with $a_{2,1}$. After the rearrangement, the board becomes $$ \begin{matrix} 2 & 1 & 1 \\ 1 & 3 & 4 \\ 1 & 4 & 3 \end{matrix} $$ Now no row or column consists of all candies with the same color.

翻译中…

In the third test case, no matter how the board is rearranged, there will always be at least one row or column consisting of all candies of color $1$. Therefore, there is no valid rearrangement.

翻译中…

样例

Input #1
3
3
1 2 3
3 1 4
4 1 2
3
1 1 1
2 3 4
1 4 3
3
1 1 1
1 1 1
1 1 2
Output #1
YES
YES
NO
InputCopy OutputCopy
题目信息
题号
2208A
时间限制
1 s
内存限制
256 MB
标签
构造
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。