Editorial for DMOPC '17 Contest 1 P6 - Land of the Carrot Trees


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.

Author: r3mark

For the first subtask, a brute-force DFS suffices.

Time complexity: \mathcal O(QN)

To obtain full points, split the operations into K blocks. For each block, precompute all connected components which exist before the block (also remove any edges mentioned in the R queries of the block before computing). Note that there are at most \frac{2Q}{K} nodes involved in the operations in the block. For each connected component containing such a node, assign this component a root and compute the XOR distance from each node in the component to the root. For each A operation in the block, make sure to update both the real edges as well as the edges between the connected components. Similarly, for each R operation, update both types of edges. Finally, for each Q operation, DFS within the connected components to obtain the answer. The final time complexity is \mathcal O(\frac{Q^2}{K}+NK). When K is \frac{Q}{\sqrt{N}}, this becomes \mathcal O(Q\sqrt{N}).

Time Complexity: \mathcal O(Q\sqrt{N})


Comments

There are no comments at the moment.