## Editorial for DMOPC '19 Contest 5 P5 - Crazy Cyclic Coincidences

**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.**

Author:

##### Subtask 1

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 or the xor-sum of the cycle. Check if is equal to either of them.

**Time complexity:**

##### Subtask 2

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 , can be obtained from the xor-sum of a subset of these basis cycles by dynamic programming.

**Time complexity:**

##### Subtask 3

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 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 of the basis cycles are going to matter. These observations are sufficient to solve this problem.

**Time complexity:** or

## Comments