Official

H - Security Camera Editorial by en_translator


First, we use meet-in-the-middle approach.
We divide the intersections into two sets, \(S\) and \(T\).

We define \(L_1[s],L_2[s]\), and \(R[t]\) as follows.

  • \(L_1[s]:\) the parity of the number of monitored roads when cameras are installed at the subset \(s\) of \(S\) (bool)
  • \(L_2[s]:\) The elements of \(T\) such that the number of roads to \(S\) \ \(s\) is odd (bitset of length \(|T|\) or int)
  • \(R [t]:\) the parity of the number of monitored roads when cameras are installed at the subset \(t\) of \(T\) (bool)

These can be computed in a total of \(O(|S| 2^{|S|}), O(|T| 2^{|T|})\) time.

Next, the number of monitored roads is even if and only if
\(L_1[s] \oplus ((\text{popcount} (L_2[s] \& t) ) \&1) \oplus R[t] = 0\).

Let us define \(t', F[t'][t]\), and \(Fsum[t']\) as follows.

  • \(t' = L_2[s]\)
  • \(F[t'][t] = ((\text{popcount} (t' \& t) ) \&1) \oplus R[t]\)
  • \(Fsum[t'] = \sum_{t \subseteq T}F[t'][t]\)

If we can find \(Fsum[t']\) for all \(t' \subseteq T\), then we can obtain the answer.
To do so, it is sufficient to find \(F[t'][t]\). However, in the first place \(F[t'][t]\) has as large as \(4^{|T|}\) number of elements, so we cannot fill all \(F\); we need to devise a good way.

Here, focusing on the most significant bit of \(t'\), we can observe the following facts.

Let \(M=2^{|T|-1}, t' \geq M\), then
\(F[t'][t] = F[t' - M][t] ( t \lt M)\),
\(F[t'][t] = 1- F[t' - M][t] ( t \geq M)\).

Here is an example for the case of \(R=(0,1,0,0,1,1,0,0)\) for an intuitive understanding. (Those in the red borders are identical, while for those in the blue borders, \(0\) and \(1\) are flipped.)

Moreover, this property holds recursively. (Next focus on the second significant bit of \(t'\).)
Therefore, we can find every \(Fsum[t']\) recursively, so that the original problem is solved in a total of \(O(N 2^{\frac{N}{2}})\) time.

Note that the operation of obtaining \(Fsum\) from \(R\) is almost the same as Hadamard Transform, and the recursive trick above is called Fast Hadamard Transform.

posted:
last update: