#P16439. [XJTUPC 2026] 鲜艳 / 方格

    ID: 18470 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 显示难度普及− 上传者: 标签>Special Judge广度优先搜索 BFS深度优先搜索 DFS连通块2026高校校赛

[XJTUPC 2026] 鲜艳 / 方格

Problem Description

Y2hlOTYw is a chess lover. However, he does not have his own chessboard.

One day, he got a piece of black-and-white grid paper with nn rows and mm columns. Of course, this is not a standard chessboard, but Y2hlOTYw does not care. He thinks that if every connected component formed by cells of the same color via 4-directional adjacency (up, down, left, right) has a rectangular shape, then this grid paper can be used as a chessboard.

Now Y2hlOTYw wants you to help determine whether this grid paper can be used as a chessboard.

Formally, let the grid paper be G={(i,j)∣1≤i≤n, 1≤j≤m}G = \{(i, j) \mid 1 \le i \le n,\ 1 \le j \le m\}, where nn and mm are positive integers. Each cell (i,j)(i,j) is assigned a color $\text{color}(i,j) \in \{\text{black},\text{white}\}$.

Two cells (i,j)(i,j) and (i′,j′)(i',j') are adjacent if and only if ∣i−i′∣+∣j−j′∣=1|i-i'| + |j-j'| = 1.

For a fixed color c∈{black,white}c \in \{\text{black},\text{white}\}, consider the subset Sc={(i,j)∈G∣color(i,j)=c}S_c = \{(i,j)\in G \mid \text{color}(i,j) = c\}. Define an equivalence relation on ScS_c: two cells are equivalent if and only if there exists a sequence of cells (i1,j1),(i2,j2),…,(ik,jk)(i_1,j_1), (i_2,j_2), \dots, (i_k,j_k) such that every cell belongs to ScS_c, and for any tt (1≤t≤k−11\le t\le k-1), cell (it,jt)(i_t,j_t) is adjacent to cell (it+1,jt+1)(i_{t+1},j_{t+1}). Each equivalence class is called a connected component. A connected component is maximal, i.e., it cannot be expanded by adding any adjacent cell of the same color.

The shape of a set of cells R⊆GR \subseteq G is called a rectangle if there exist integers r1≤r2r_1 \le r_2 and c1≤c2c_1 \le c_2 such that:

$$R = \{(i,j) \mid r_1 \le i \le r_2,\ c_1 \le j \le c_2\}$$

Now you need to determine whether the shape of every connected component is a rectangle.

Input Format

This problem contains multiple test cases. The first line of the input contains a positive integer TT (1≤T≤82661\le T\le 8266), indicating the number of test cases.

Next are the descriptions of the TT test cases.

The first line of each test case contains two integers nn and mm (1≤n,m≤5001 \le n,m \le 500), separated by a space, indicating that the grid paper has nn rows and mm columns.

The next nn lines: the ii-th line contains a string SiS_i of length exactly mm, describing the given grid paper. It is guaranteed that SiS_i contains only characters 0\texttt{0} and 1\texttt{1}. For any integers ii and jj (1≤i≤n,1≤j≤m1\le i\le n, 1 \le j \le m):

  • If the jj-th character of the ii-th line is 1\texttt{1}, then the cell (i,j)(i,j) in row ii and column jj is black (black\text{black}).
  • If the jj-th character of the ii-th line is 0\texttt{0}, then the cell (i,j)(i,j) in row ii and column jj is white (white\text{white}).

It is guaranteed that the sum of n⋅mn \cdot m over all test cases does not exceed 2.5×1052.5 \times 10^5.

Output Format

For each test case, output one line containing a string:

  • If this grid paper can be used as a chessboard, i.e., the shape of every connected component is a rectangle, output Yes\tt{Yes}.
  • Otherwise, output No\tt{No}.

The answer is case-insensitive. For example, yEs\tt{yEs}, Yes\tt{Yes}, yes\tt{yes}, and YES\tt{YES} will all be considered as Yes\tt{Yes}.

4
2 3
110
110
4 3
110
111
111
111
5 5
00000
01110
01010
01110
00000
8 8
01010101
10101010
01010101
10101010
01010101
10101010
01010101
10101010
Yes
No
No
Yes

Hint

Translated by ChatGPT 5