#K34707. Rearranging the Garden
Rearranging the Garden
Rearranging the Garden
You are given a garden represented as an n × m grid of integers. Each integer represents the type of plant at that cell. You are allowed to rearrange the garden only by permuting (i.e. reordering) its rows. Your task is to determine whether it is possible to rearrange the rows such that in the resulting garden, every row and every column contains all distinct plant types.
In other words, after reordering the rows, for every row, all m elements should be distinct, and for every column, all n elements should be distinct. Formally, if the rearranged garden is denoted by \(A = [a_{ij}]\), the following conditions must hold:
- \(\forall i,\; \{a_{i1},a_{i2},\ldots,a_{im}\}\) has size \(m\).
- \(\forall j,\; \{a_{1j},a_{2j},\ldots,a_{nj}\}\) has size \(n\).
If a valid rearrangement exists, output "Yes" followed by the rearranged garden; otherwise, output "No".
inputFormat
The first line of input contains two integers \(n\) and \(m\), representing the number of rows and columns in the garden.
Each of the next \(n\) lines contains \(m\) space-separated integers representing the garden matrix.
outputFormat
If a valid rearrangement exists, output "Yes" on the first line, followed by \(n\) lines. Each of these lines should contain \(m\) space-separated integers representing a valid rearrangement of the garden. If no valid rearrangement exists, output only "No".
## sample3 3
1 2 3
3 1 2
2 3 1
Yes
1 2 3
3 1 2
2 3 1
</p>