Almost Fault-Tolerant Database
Contest: Codeforces Round 704 (Div. 2)
Difficulty rating: 2500
Tags: brute force, constructive algorithms, dfs and similar, greedy, implementation

You are storing an integer array of length $$$m$$$ in a database. To maintain internal integrity and protect data, the database stores $$$n$$$ copies of this array.

Unfortunately, the recent incident may have altered the stored information in every copy in the database.

It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database.

In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well.

Input format:
The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 \le n$$$; $$$1 \le m$$$; $$$n \cdot m \le 250\,000$$$) — the number of copies and the size of the array.

Each of the following $$$n$$$ lines describes one of the currently stored copies in the database, it consists of $$$m$$$ integers $$$s_{i, 1}, s_{i, 2}, \dots, s_{i, m}$$$ ($$$1 \le s_{i, j} \le 10^9$$$).

Output format:
If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length $$$m$$$ and contain integers between $$$1$$$ and $$$10^9$$$ only.

Otherwise, print "No".

If there are multiple possible arrays, print any of them.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
In the first example, the array $$$[1, 10, 1, 100]$$$ differs from first and second copies in just one position, and from the third copy in two positions.

In the second example, array $$$[1, 1, 1, 1, 1, 1, 1]$$$ is the same as the first copy and differs from all other copies in at most two positions.

In the third example, there is no array differing in at most two positions from every database's copy.

Example input:
3 4
1 10 10 100
1 1 1 100
10 100 1 100

Example output:
Yes
1 10 1 100

Example input:
10 7
1 1 1 1 1 1 1
1 1 1 1 1 1 2
1 1 1 1 1 2 2
1 1 1 1 2 2 1
1 1 1 2 2 1 1
1 1 2 2 1 1 1
1 2 2 1 1 1 1
2 2 1 1 1 1 1
2 1 1 1 1 1 1
1 1 1 1 1 1 1

Example output:
Yes
1 1 1 1 1 1 1

Editorial:
Let's look at the first array. Every possible answer (if there is any) should not differ from this array in more than $$$2$$$ positions. So we should try to change at most $$$2$$$ positions in the first array to find a consistent answer.
Let's look at the other arrays. We can ignore each other array that differ with the first array in no more than $$$2$$$ positions. In particular, if every array doesn't differ in more than $$$2$$$ position, we can just print the first array as an answer.
If an array differs in $$$5$$$ (or more) positions, then no answer exists.
Suppose that we selected an array with $$$3$$$ or $$$4$$$ diffs. This means that we must change $$$1$$$ or $$$2$$$ positions (respectively) in the first array to reduce it to $$$2$$$ diffs. We have $$$3$$$ or $$$6$$$ ways to do this (respectively), and can go through all the options.
After changing, we should look at all arrays again and make changes in the first array again if required (but no more than in $$$2$$$ positions in total) in a recursive backtracking manner. If we don't get an answer, we should go back and try another option.
The time complexity is $$$O(n \cdot m)$$$ with a decent constant.
Bonus1: If allowed number of diffs is $$$k$$$, can you solve problem in $$$O(k^knm)$$$ time?
Bonus2: If allowed number of diffs is $$$k$$$, can you solve problem in $$$O(nm + k^k \sqrt{nm})$$$ time?