Editorial for Facebook Hacker Cup '15 Round 3 P4 - Fox Rocks


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.

Let's consider the given directed graph of clearings and trails to be divided into M = \text{ceil}(N/4) rows, with nodes 0 \dots 3 in row 1, nodes 4 \dots 7 in row 2, and so on. The constraint that an edge from node a to node b may only exist if 0 \le \text{floor}(b/4)-\text{floor}(a/4) \le 1 can then be restated as follows: an edge from node a to node b may only exist if a and b are in the same row, or if b is in the row just after a's row.

When taking a stroll starting from a node in row r, note that Mr. Fox will walk amongst nodes in row r, walk to a node in row r+1, walk amongst nodes in row r+1, walk to a node in row r+2, 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 4 \times 4 table of values P, where P_{i,j} is the probability that, given that Mr. Fox is currently in the i^\text{th} node in row r, he will eventually reach the j^\text{th} node in row r+1 without having reached any other nodes in row r+1 previously. Note that \sum_{j=1}^4 P_{i,j} may not be equal to 1, if there's a chance that Mr. Fox will never reach row r+1 from the i^\text{th} node in row r.

This table can be calculated by modeling the 8 nodes in rows r and r+1 (which we can re-number as nodes 1 \dots 4 and 5 \dots 8 respectively) as a Markov chain, with nodes 5 \dots 8 (and any nodes with no outgoing edges) being absorbing states. In particular, we can construct an 8 \times 8 one-step transition matrix A, where A_{i,i} = 1 if node i is an absorbing state, A_{i,j} = 0 if node i is an absorbing state and i \ne j, and A_{i,j} is the weighed probability of Mr. Fox walking to node j while at node i (based on the relative rock counts on outgoing edges from node i) if i is not an absorbing state. The long-run transition matrix B can then be computed by using fast matrix exponentiation to take A to a high enough power (for example, B = A^{2^{16}}). P then consists of the values B_{i,j} for i = 1 \dots 4 and j = 5 \dots 8. This computation takes 16 \times 8^3 time per table. P can be computed more efficiently (with 8^3 time) with Gaussian elimination and some additional operations, but this is not required.

The above P 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 a is added or removed (due to an event of type 1 or 2), the P table for node a's row will have to be recomputed. Therefore, the total time spent on them will be (M+D) \times 16 \times 8^3, which is expensive but acceptable.

Now, to handle events of type 3, let's say that Mr. Fox starts at the {c_1}^\text{th} node in row r_1, and we want the probability that he'll eventually visit the {c_2}^\text{th} node in row r_2. If r_2 < r_1, then the answer is trivially 0. Otherwise, the first step is to determine a list of 4 values S similar to the P table, where S_i is the probability that Mr. Fox will eventually reach the i^\text{th} node in row r_2 without having reached any other nodes in row r_2 previously. In fact, if we consider the product of matrices P_{r_1} \times P_{r_1+1} \times \dots \times P_{r_2-1} (or the identity matrix if r_1 = r_2), then S is just the {c_1}^\text{th} row of this resulting matrix.

We can't afford to loop over \mathcal O(M) rows to compute S for each type 3 operation, but the standard concept of using a segment tree to perform range queries will cut that factor down to \mathcal O(\log M). In this case, each node in the segment tree will store a 4 \times 4 matrix of probabilities – the leaf nodes of the tree will contain the computed P 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 M \times 4^3 time, updating it when a P table changes takes \log(M) \times 4^3 time, and querying it to compute S also takes \log(M) \times 4^3 time. Therefore, the total time from these calculations is (M + D \log M) \times 4^3.

Finally, given S, we still need to consider Mr. Fox walking amongst the nodes in row r_2 to determine the probability that he'll ever visit its {c_2}^\text{th} node. Just like the P table calculation, this may be modeled as a Markov chain, with an 8 \times 8 one-step transition matrix A calculated exactly as before but with node c_2 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 B = A^{2^{16}} may also be calculated as before, in 16 \times 8^3 time, and then the final answer will be \sum_{i=1}^4 S_i \times B_{i,c_2}, considering each of the 4 possible points of entry into row r_2.


Comments

There are no comments at the moment.