Editorial for TLE '16 Contest 2 P6 - The Great Search
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
We can do some simple math. If the girl is not in node 1, the CS nerd is guaranteed to find her in node 2. Therefore, we simply need to find the average time to get to node 2 and divide that result by 2 since there is a 50% chance that the girl is in node 1, which takes 0 time, and another 50% chance that the CS nerd has to take a hallway to node 2.
Time complexity: 
Subtask 2
We need a bit more knowledge on how expected values work. For the CS nerd to reach node 2, let  be the expected time from node 1 to node 2. Also, let 
 be the expected time from node 3 to node 2. Obviously, 
.
Then an equation can be created if the CS nerd is simulated to move once from node 1.
 is the chance that the CS nerd will travel from 
 to 
.
In this way, 3 equations can be created (an equation is ). 
 can be solved, and this is the first linear system.
The girl may be at node 3, so 2 linear systems have to be solved in total. The answer is the average of the three possible times.
Time complexity: 
Subtask 3
Generate the equations from above. There would be  systems to solve in total. Use Gaussian elimination to solve each of these 
 systems, but note that the runtime of Gaussian elimination is 
 since a system contains 
 variables. The answer is the average of the 
 possible times.
Time complexity: 
Subtask 4
It is important to treat the graph as a Markov chain where each edge contains a probability and a time. The simplest way is to create two matrices containing  elements.
Observe that a node can be removed from the graph, while maintaining the same expected value. Let the removed node be labelled , then any edge pointing to 
 must exit it somewhere else in the graph. The edge pointing to 
 can be replaced by 
 other edges, and the process of combining duplicate edges is left as an exercise. After 
 has an indegree of 0, 
 can be removed from the graph without changing the expected value to any remaining node in the graph.
There is another bit of cleanup after  is removed. There may now be 
 selfloops in the graph. Since only edges leading out of a node are important, a selfloop can be cleaned up in 
 time. The implementation is also left to the reader. Therefore, the total time complexity of removing a node is 
.
To get the expected time to a specific node,  removals must take place. Note that the time complexity of this method is 
, which is equal to Gaussian elimination.
The last observation is that by doing  operations in total, a lot of work is repeated. For example, getting the answer to node 2 and node 3 would require many of the same nodes to be removed, and therefore the time complexity can be improved.
One possible way is to create a divide-and-conquer algorithm that takes in a range  and returns the value 
. Notice that half of these nodes can be removed, then the divide-and-conquer algorithm can be called again (after the 
 elements in the matrix are duplicated). In this algorithm 
 nodes are removed in total, and 
 matrices are duplicated in total. The implementation of this algorithm is left to the reader as an exercise.
We can make further optimizations to reduce the total time complexity to  because a divide-and-conquer creates two matrices with half the size. That is, suppose your algorithm takes 
 steps where 
. Then the Master Theorem proves that 
.
Time complexity:  or 
Comments