Editorial for Wesley's Anger Contest 5 Problem 4 - Prison Escape
Submitting an official solution before solving the problem yourself is a bannable offence.
Authors:
,Subtask 1
We can model the prison as a graph where the vertices are the rooms and the edges are the passageways. For each query, we can perform a depth first search to get all vertices on the path from to (ignoring ) and all vertices on the path from to . We can then find the first index in the two paths where the vertices are the same.
Time Complexity:
Subtask 2
In this subtask, the graph is a line. We can deal with the queries by checking the midpoint of and (if it exists) and seeing if it will be reached before Besley reaches and the guard reaches and if they will meet that midpoint at the same time. It can be seen that there is no other point where they can meet.
Time Complexity:
Subtask 3
For each vertex, we can store the minimum distance to any vertex on the path from to , as well as the distance to vertex . We can see that the answer only exists if the distance of vertex from vertex is equal to the distance of vertex from vertex , and the distance from vertex to the path is not equal to the distance from vertex to vertex . If these conditions are satisfied, then the answer is equal to the distance from vertex to the path.
Time Complexity:
Subtask 4
In this subtask, Besley and the guard will meet only if the path from to intersects the path from to . If the tree is rooted at vertex , then they only intersect if the lowest common ancestor of and is on the path from to (it is possible to do this without traditional lowest common ancestor algorithms). It can be seen that when this happens, the path from to will intersect with the path from to over some path of vertices. We can then use a method similar to subtask 2 to determine if they will meet on this path, adjusting for the time the guard first reaches the path from to .
Time Complexity:
Subtask 5
In this subtask, we can root the tree at vertex . Besley and the guard will only meet if the depth of and are the same. They will first meet at their lowest common ancestor as long as it is not vertex . The answer is the difference between the depth of and the lowest common ancestor of and .
Time Complexity:
Subtask 6
Special thanks to
for providing the idea behind this solution.It can be proven that if a solution exists, it will always be the middle vertex on the path from to . can be found by first finding the distance between the two vertices using the lowest common ancestor (), and then selecting the middle vertex in using any standard method (binary lifting, heavy light decomposition, etc…). The distance between two vertices and in a tree is . Here, a solution exists if and only if is on both the path from to and on the path from to and is not or . We can check if vertex is on a path from vertices to by checking if the set is the same as the set . We can query for the lowest common ancestor () in time with or precomputation using any data structure that supports range minimum queries over Euler tour indices.
Time Complexity:
Comments