Editorial for Facebook Hacker Cup '15 Round 3 P4 - Fox Rocks
Submitting an official solution before solving the problem yourself is a bannable offence.
Let's consider the given directed graph of clearings and trails to be divided into rows, with nodes in row , nodes in row , and so on. The constraint that an edge from node to node may only exist if can then be restated as follows: an edge from node to node may only exist if and are in the same row, or if is in the row just after 's row.
When taking a stroll starting from a node in row , note that Mr. Fox will walk amongst nodes in row , walk to a node in row , walk amongst nodes in row , walk to a node in row , and so on until he reaches a closed set of nodes in some row which have no outgoing edges to the following row, which he'll walk amongst forever (or until he reaches a node with no outgoing edges, at which point the stroll will end).
For each row, we'd like to calculate a table of values , where is the probability that, given that Mr. Fox is currently in the node in row , he will eventually reach the node in row without having reached any other nodes in row previously. Note that may not be equal to , if there's a chance that Mr. Fox will never reach row from the node in row .
This table can be calculated by modeling the nodes in rows and (which we can re-number as nodes and respectively) as a Markov chain, with nodes (and any nodes with no outgoing edges) being absorbing states. In particular, we can construct an one-step transition matrix , where if node is an absorbing state, if node is an absorbing state and , and is the weighed probability of Mr. Fox walking to node while at node (based on the relative rock counts on outgoing edges from node ) if is not an absorbing state. The long-run transition matrix can then be computed by using fast matrix exponentiation to take to a high enough power (for example, ). then consists of the values for and . This computation takes time per table. can be computed more efficiently (with time) with Gaussian elimination and some additional operations, but this is not required.
The above table calculations will need to be done for each row initially (once the starting edges have been inputted), and then each time an edge from some node is added or removed (due to an event of type 1 or 2), the table for node 's row will have to be recomputed. Therefore, the total time spent on them will be , which is expensive but acceptable.
Now, to handle events of type 3, let's say that Mr. Fox starts at the node in row , and we want the probability that he'll eventually visit the node in row . If , then the answer is trivially . Otherwise, the first step is to determine a list of values similar to the table, where is the probability that Mr. Fox will eventually reach the node in row without having reached any other nodes in row previously. In fact, if we consider the product of matrices (or the identity matrix if ), then is just the row of this resulting matrix.
We can't afford to loop over rows to compute for each type 3 operation, but the standard concept of using a segment tree to perform range queries will cut that factor down to . In this case, each node in the segment tree will store a matrix of probabilities – the leaf nodes of the tree will contain the computed tables, and each interior node will store the product of its left child's matrix with its right child's matrix. Building the tree initially takes time, updating it when a table changes takes time, and querying it to compute also takes time. Therefore, the total time from these calculations is .
Finally, given , we still need to consider Mr. Fox walking amongst the nodes in row to determine the probability that he'll ever visit its node. Just like the table calculation, this may be modeled as a Markov chain, with an one-step transition matrix calculated exactly as before but with node also being made into an absorbing state (by ignoring all of its outgoing edges and giving it a self loop). The long-run transition matrix may also be calculated as before, in time, and then the final answer will be , considering each of the possible points of entry into row .
Comments