Editorial for DMOPC '19 Contest 5 P5 - Crazy Cyclic Coincidences
Submitting an official solution before solving the problem yourself is a bannable offence.
Notice for this subtask that for a path to start and end at the same city, the xor-sum of the edges used must be equal to either ~0~ or the xor-sum of the cycle. Check if ~V~ is equal to either of them.
Time complexity: ~\mathcal O(N)~
For the second subtask, we observe that there always exist a subset of cycles that we shall refer to as a basis such that any path that starts and ends at the same city is equivalent to the xor-sum of exactly one subset of the basis cycles. One such basis can be created by first forming a rooted spanning tree, and for each edge not part of the spanning tree, we add to the basis the cycle that consists of the xor of the edge and the tree paths from the root to the two endpoints of the edge. Afterwards, the value of each of the basis cycles can be determined. Finally, we can check whether or not the value ~V~, can be obtained from the xor-sum of a subset of these basis cycles by dynamic programming.
Time complexity: ~\mathcal O(MV)~
For the third subtask, we observe that while doing the dynamic programming in Subtask 2, that a cycle can only contribute to the set of achievable xor sums if its value cannot be obtained by the xor-sum of a subset of the already considered cycles. This can be checked in ~\mathcal O(1)~ time while doing the dynamic programming. Furthermore, each time such a cycle is added, it can be proven that the total number of reachable values by xor-sum must be doubled. Thus, at most ~\mathcal O(\log V)~ of the basis cycles are going to matter. These observations are sufficient to solve this problem.
Time complexity: ~\mathcal O(M+V\log V)~ or ~\mathcal O(M+V)~