Editorial for COCI '12 Contest 3 #6 Procesor


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Let us consider the N 32-bit registers as 32N binary variables. At first glance, the problem looks like a textbook 2SAT problem example. However, given the small time and memory limits, such a solution isn't efficient enough to obtain all points for the task.

Notice that, if any solution exists, at least one more solution must exist, and it can be obtained by inverting the bits of all the registers from the first solution.

It follows that the following algorithm is correct:

  1. Find a still unset binary variable b.
  2. Set the value of b arbitrarily (to either 0 or 1).
  3. Set the value of all binary variables that were ever XOR-ed with b, since we can now determine their value unambiguously.
  4. If there are no unset variables left, stop; otherwise, return to step 1.

If, in step 3, we try to set a variable that is already set to the opposite value, we have found a contradiction and there is no valid solution. Notice that setting another value in step 2 would again lead to a contradiction in the same step 3, since XOR implications are bidirectional.

In the beginning, we can therefore set any variable to either 0 or 1, because there are at least two solutions with opposite values of all variables. After the other three steps of the algorithm, we have a smaller remaining set of unset variables which has no relation to the already set variables, so we can apply the same rule (there are at least two solutions) and set any variable to either 0 or 1 and repeat the procedure.

We will choose the unset variable and its value in step 1 in such a way to obtain the lexicographically smallest solution: we find the most significant unset bit of the first register which still has unset bits, set its value to 0 and continue with step 2.

The small memory limit requires implementing step 3 iteratively (using, for example, a queue). The time complexity is \mathcal O(N+E).


Comments

There are no comments at the moment.