Reconstruct a binary matrix from the matrix of OR-values over all rows and columns.
You are given an matrix consisting of $0 and \1$.
Interpret as the result of the following process on an unknown binary matrix of the same size:
- If , then every cell in row and every cell in column of must be $1$.
- If , then there must be at least one cell equal to $0i and at least one cell equal to \0j$.
Your task is to determine whether such a binary matrix exists. If it exists, output one valid matrix; otherwise, report that it is impossible.
This is the classic reconstruction task behind the "OR in Matrix" problem.
Input Format
- The first line contains two integers and .
- The next lines contain integers each, describing matrix .
The exact official limits are not repeated here; solve for a general binary matrix.
Output Format
- If no valid matrix exists, output
NO. - Otherwise output
YESfollowed by any valid binary matrix $A`.
A valid matrix should satisfy all constraints implied by .
Constraints
- Output any one valid solution if it exists.
Example 1
Input
2 2 1 1 1 1
Output
YES 1 1 1 1
Explanation
All entries in are 1, so the all-ones matrix is a valid reconstruction.
Example 2
Input
3 3 1 1 1 1 0 1 1 1 1
Output
NO
Explanation
The center cell is 0, so its row and column must each contain a 0. That would force additional zeros in , so this configuration cannot come from any valid binary matrix.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.