#P16444. [XJTUPC 2026] Used to be

    ID: 18475 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>贪心并查集Special Judge2026高校校赛

[XJTUPC 2026] Used to be

Problem Description

"Who am I...?"

This is a story about memory. One of the protagonists is a 01 string of length mm.

The first layer of the story is change.

The story travels through n−1n-1 nodes. Each time it passes a node, the protagonist is subtly affected: it may stay unchanged, or it may flip at exactly one position. These changes keep accumulating, and the final string may become completely different from the original.

The second layer of the story is forgetting.

Long ago, the script recorded the protagonist's initial state, as well as the state after passing each node. However, as time went by, the handwriting at some positions on the pages gradually blurred and turned into unreadable ?.

The third layer of the story is searching.

Another group of protagonists—you—found this dusty record. Even though you may not be able to determine the only truth, you still hope to piece together one possible original appearance and recreate the story in your mind.

"Make her story complete."

"I know... you will not disappoint me."

Formal statement

Given nn strings S1,S2,⋯ ,SnS_1, S_2, \cdots, S_n, each of length exactly mm. Each string SiS_i contains only characters 0\texttt{0}, 1\texttt{1}, and ?\texttt{?}.

Determine whether there exists a way to replace each ?\texttt{?} independently with 0\texttt{0} or 1\texttt{1} such that any two adjacent strings differ in at most one position. That is, after replacement, for any ii (1≤i≤n−11\le i\le n-1), the ii-th string Si=a1a2⋯amS_i=a_1a_2\cdots a_m and the (i+1)(i+1)-th string Si+1=b1b2⋯bmS_{i+1}=b_1b_2\cdots b_m satisfy one of the following:

  • For all jj (1≤j≤m1\le j\le m), aj=bja_j=b_j;
  • There exists an xx (1≤x≤m1\le x\le m) such that ax≠bxa_x\ne b_x, and for all jj (1≤j≤m1\le j\le m and x≠jx\ne j), aj=bja_j=b_j.

If it exists, output one such scheme. If there are multiple schemes, output any one.

Input Format

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

The following are TT test cases.

The first line of each test case contains two positive integers nn and mm (1≤n⋅m≤1061 \le n\cdot m \le 10^6), separated by a space, representing the number of strings and the length of each string.

Then follow nn lines. The ii-th line contains a string SiS_i of length exactly mm. It is guaranteed that SiS_i contains only characters 0\texttt{0}, 1\texttt{1}, and ?\texttt{?}.

It is guaranteed that the sum of n⋅mn\cdot m over all test cases does not exceed 10610^6.

Output Format

For each test case, if there is no valid scheme, output one line containing only the string No\tt{No}.

Otherwise, output n+1n+1 lines, where:

  • The first line contains the string Yes\tt{Yes}.
  • The next nn lines: the ii-th of these lines contains a binary string of length mm containing only 0\texttt{0} and 1\texttt{1}, representing the ii-th string in the constructed scheme.

If there are multiple valid schemes, output any one.

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

3
2 3
000
010
3 3
010
1?1
010
3 4
00?1
01?1
1?01
Yes
000
010
No
Yes
0001
0101
1101

Hint

Translated by ChatGPT 5