New Road Network
Contest: Mail.Ru Cup 2018 Round 1
Difficulty rating: 3300
Tags: constructive algorithms, greedy, math

The king of some country $$$N$$$ decided to completely rebuild the road network. There are $$$n$$$ people living in the country, they are enumerated from $$$1$$$ to $$$n$$$. It is possible to construct a road between the house of any citizen $$$a$$$ to the house of any other citizen $$$b$$$. There should not be more than one road between any pair of citizens. The road network must be connected, i.e. it should be possible to reach every citizen starting from anyone using roads. To save funds, it was decided to build exactly $$$n-1$$$ road, so the road network should be a tree.

However, it is not that easy as it sounds, that's why the king addressed you for help. There are $$$m$$$ secret communities in the country, each of them unites a non-empty subset of citizens. The king does not want to conflict with any of the communities, so he wants to build the network such that the houses of members of each society form a connected subtree in network. A set of vertices forms a connected subtree if and only if the graph remains connected when we delete all the other vertices and all edges but ones that connect the vertices from the set.

Help the king to determine if it is possible to build the desired road network, and if it is, build it.

Input format:
Each test consists of one or more test cases.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2000$$$) — the number of test cases.

The following lines describe the test cases, each in the following format.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 2000$$$) — the number of citizens and the number of secret communities.

The next $$$m$$$ lines contain the description of the communities. The $$$i$$$-th of these lines contains a string $$$s_i$$$ of length $$$n$$$, consisting of characters '0' and '1'. The citizen with number $$$j$$$ is a member of the $$$i$$$-th community if and only if $$$s_{{i}{j}}=1$$$. It is guaranteed that the string $$$s_i$$$ contains at least one character '1' for each $$$1 \leq i \leq m$$$.

It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2000$$$ and the sum of $$$m$$$ for all test cases does not exceed $$$2000$$$.

Output format:
Print the answer for all test cases in the order they are given in the input, each in the following format.

If there is no way to build the desired road network, print "NO" (without quotes).

Otherwise in the first line print "YES" (without quotes).

In the next $$$n-1$$$ lines print the description of the road network: each line should contain two integers $$$a$$$ and $$$b$$$ ($$$1 \leq a, b \leq n$$$, $$$a \neq b$$$) that denote you build a road between houses of citizens $$$a$$$ and $$$b$$$.

The roads should form a connected tree, and each community should form a connected subtree.

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

Note:
In the first example you can build the following network:

It is easy to see that for each community all the houses of its members form a connected subtree. For example, the $$$2$$$-nd community unites the citizens $$$1$$$, $$$2$$$, $$$3$$$. They form a connected subtree, because if we delete everything except the houses $$$1$$$, $$$2$$$, $$$3$$$ and the roads between them, two roads will remain: between $$$1$$$ and $$$3$$$ and between $$$2$$$ and $$$3$$$, forming a connected graph.

There is no network in the second example suitable for the king.

Example input:
2
4 3
0011
1110
0111
3 3
011
101
110

Example output:
YES
1 3
2 3
3 4
NO

Editorial:
Let us denote for $$$A_i$$$ the set of sets (or secret сomunities, as in our problem), which contains $$$i$$$-th vertex.
Note that if some set $$$A_i$$$ contains an element that does not belong to any other set $$$A_j$$$, it can be thrown out and it will not affect to the answer. Let's make this.
Let the tree exist. Then it has a leaf (let this vertex $$$i$$$). Let the only edge from $$$i$$$ be to vertex $$$j$$$. Any element $$$A_i$$$ is contained in $$$\geq 2$$$-x sets and in the tree all sets (which secret сomunities) form connected subtrees. This implies that $$$A_i \subset A_j$$$. So, if in $$$A_1, \ldots, A_n$$$ there are no two sets such that one subset of the other, then the tree does not exist. Otherwise, let us find any pair $$$A_i \subset A_j$$$. We prove that there exists a tree in which $$$i$$$ is a leaf that has an edge to $$$j$$$. It is possible to change the edges of any tree-solution that $$$i$$$ becomes a leaf that has an edge to $$$j$$$, but the tree will still be a solution (it is the exercise for the reader). Let's find a good pair $$$(i, j)$$$, remove $$$i$$$ and continue the process. At the same time, at each step, it is necessary to remove the elements contained in $$$\leq 1$$$ sets.
If we implement such an algorithm in this form it will work for $$$O (n^3 \cdot m / 64)$$$ time, which is too big. To improve the algorithm's complexity, for each set $$$A_i$$$, we store a set of indices $$$j$$$, such that $$$A_i \subset A_j$$$. Then at each step, you can quickly find the needed pair $$$(i, j)$$$. At the same time, removing $$$i$$$, you can quickly update this structure.
In this problem, there is a simpler solution, but it is more difficult to prove it. Let's build a weighted undirected graph on $$$n$$$ vertices with weight $$$w_{{i}{j}} = |A_i \cap A_j|$$$. Let's construct the maximum spanning tree in this graph. It is can be proved that either it is the answer, or there is no solution. This can be understood from the proof of the previous solution, which essentially simulates the search for such a spanning tree.
Time complexity: $$$O (n^2 \cdot m / 64)$$$.