Editorial for Facebook Hacker Cup '19 Final Round P6 - Temporal Revision


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.

We'll begin by considering the set of meaningfully different states which the Enterprise can be in, specifically in terms of which graph nodes (planets) it may visit. Before hour 1, all nodes form a single connected component, and the Enterprise may travel freely between them. Then, each time a type-1 event i causes nodes A[V[i]] and B[V[i]] to no longer be reachable from one another, the component they were both part of becomes split into two, such that if the Enterprise was in that component at hour i, it must henceforth choose to be in one of the two resulting components. This suggests a binary tree structure of state nodes, each of which corresponds to a component and a time interval for which that component exists.

If we iterate over the K events in reverse while maintaining a disjoint-set data structure of components (initialized with all components which exist after hour K), we can reconstruct this implicit binary tree in \mathcal O(K \alpha(N)) time. Each time a type-1 event causes two different components to merge together, their corresponding tree nodes should be the children of a new tree node corresponding to the combined component.

It is then feasible to consider some dynamic programming on this tree, ignoring the ability to travel back in time for now. Let's initially consider the basic question: "what's the maximum number of geyser activations which the Enterprise can be present for if it begins in tree node i at that node's earliest time?". Letting this value be DP[i], we have DP[i] = \{\text{number of geyser activations occurring within node }i\} + \max\{DP[\text{any child of }i]\}. Let's next consider a more complex and useful question: "what's the maximum number of geysers from which the Enterprise can collect neurozine if it begins in tree node i at time t?". This is equal to DP[i] + \{\text{number of geysers in node }i\text{ which are already active going into it}\} - \{\text{number of geyser deactivations within node }i\text{ before time }t\}. So far, these are all relatively simple values which we can precompute while constructing the tree (still in \mathcal O(K \alpha(N)) time) and then evaluate in \mathcal O(\log N) time (due to binary searching through geyser deactivation times). To wrap this up into the ability to answer a query (still ignoring traveling back in time), all that remains is efficiently computing the tree node corresponding to a given node x at a given time y, which is doable in \mathcal O(\log N) time if we begin at the leaf tree node corresponding to x and then binary search up a skip-list of that tree node's ancestors (which can be precomputed throughout the tree in \mathcal O(N \log N) time).

We're ready to add in some time travel — what if you may only travel back to the original time y? This may be thought of as the Enterprise's path downwards through the tree being allowed to branch off at most once. As such, we can add an extra flag to our DP (DP[\text{tree node}][1\text{ if still allowed to branch, or }0\text{ otherwise}]), and then evaluate a similar expression to before (with DP[i][1] this time).

Finally, we'll add in the ability to travel back to time y-24 (noting that there's no benefit to travelling to later than that). One possibility is that the Enterprise will pass through tree node i again after going back in time, and branch off later — in this case, we'll have DP[i][1] plus the number of other active geysers which the Enterprise had access to between times y-24 and y-1, which are possible (though not trivial) to count in \mathcal O(\log N) time using the same precomputed values as described above. The other possibility is that the Enterprise will branch off at an earlier point (from one of the ancestors of tree node i) — for each such possible ancestor a, we'll have DP[i][0], plus DP[a\text{'s other child}][0], plus the number of other active geysers which the Enterprise had access to between times y-24 and the end of tree node a (which are similarly possible to count along the way). The query's answer is then the maximum of values obtained from any of these possibilities, and we have a solution with time complexity \mathcal O((Q+N) \log N + K \alpha(N) + M).


Comments

There are no comments at the moment.