Editorial for COCI '19 Contest 6 #4 Skandi
Submitting an official solution before solving the problem yourself is a bannable offence.
In order to score points on the first subtask, it was enough to note that, due to the small number of ones, the number of questions in our crossword puzzle is very small. It was enough to use brute force in order to traverse each subset of those questions and assume that was the subset of questions that needed to be answered. For each subset, we check whether the crossword puzzle was filled. The time complexity is , where denotes the total number of questions.
The constraints of the second subtask will immediately put an experienced contestant on the right track. One dimension of the matrix is very small and the problem immediately starts smelling like dynamic programming with bitmasks. And that intuition is correct, we will traverse the matrix row-by-row and store in our bitmask in which columns we have decided to answer vertical questions whose answers span through the current row. Therefore, the state can be denoted with and it tells us what is the minimal number of questions that need to be answered in order to fill an entire crossword puzzle if we have filled all rows until and we are currently at row with storing active columns as described. Further analysis of the transitions and the reconstruction are left as an exercise to the reader.
Sometimes it is useful to obtain a more formal description of the problem. In this case, that will naturally lead us to the full solution. Let's denote the horizontal questions with , and vertical questions with . Let (or ) be equal to if we choose to answer that particular question in our solution and otherwise. Note that for each empty square we need to choose to answer at least one of exactly two questions which have answers that contain . Therefore, for each empty square there are exactly two questions and for which must hold.
Let's model this with a bipartite graph in which on one side we have nodes that represent horizontal questions, and on the other side we have nodes that represent vertical questions. Let's connect two nodes that represent questions and with an edge if there exists an empty square for which must hold. Now, we want to determine the smallest subset of nodes such that each edge is incident to at least one node from that subset. This is a famous problem called minimum vertex cover. Since the graph is bipartite, we can use Kőnig's theorem and solve the problem in polynomial complexity.
Comments