Editorial for COCI '12 Contest 3 #6 Procesor
Submitting an official solution before solving the problem yourself is a bannable offence.
Let us consider the
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:
- Find a still unset binary variable
. - Set the value of
arbitrarily (to either or ). - Set the value of all binary variables that were ever XOR-ed with
, since we can now determine their value unambiguously. - 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
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
The small memory limit requires implementing step 3 iteratively (using, for example, a queue). The time complexity is
Comments