Editorial for Baltic OI '13 P5 - Tracks in the Snow
Submitting an official solution before solving the problem yourself is a bannable offence.
Greedy Approach
Remark 1. The animal that crossed the meadow last can be deduced directly from the input: it is simply the animal that left the topmost tracks in the upper left (or, alternatively, lower right) corner.
Remark 2. In an optimal solution (using the minimum number of animals), no two animals of the same type will cross the meadow directly after each other.
Proof. We could simply merge the two paths (go along the first one from the upper left to the lower right corner, return on the same way and take the second path down) to get a new one, thereby reducing the number of animals used. 
In any step, the current animal can visit all the cells reachable from the lower left corner using only already visited cells and cells containing his respective tracks. Now consider the following greedy algorithm: at any step, we simply visit all reachable cells, as long as there are unvisited cells (this algorithm processes the animals in reverse order). Note that the animal we choose at each step is given by the remarks above.
Proposition 3. The described algorithm is correct and uses the minimum number of animals possible.
Proof. We can clearly visit all those cells and since there is a way from source to target completely covered with tracks from the last animal, we can simply go back to the source and follow that way. Thus the result of the algorithm corresponds to a legal set of animal moves.
We now show by induction that, for any , after having used 
 animals we have visited all the cells possible. The case 
 follows by definition. Now assume 
. Let 
 be the set of all cells that could be reached by using 
 animals but isn't visited by the above algorithm using only 
 animals. If 
 we're finished. Otherwise, take the cell 
 with the minimum distance to the upper left corner (measured in the minimum number of cells on any valid path to this cell). Then by choice, all other cells on this path are visited by our algorithm. If the cell considered were reachable by less than 
 animals, we would have already visited it. So according to the remarks above, the topmost track on the cell is the same we are currently considering in our algorithm, so our algorithm will visit it contradicting the choice of 
. So 
 is indeed empty and thus our algorithm is indeed optimal. 
A simple way to implement this algorithm is to set all cells visited so far to a special "Don't care" character (e.g. *). This algorithm needs  steps for an 
 meadow and suffices for the first subtask. The figure below shows that there are indeed up to 
 animals needed.
Linear solution
Remove from the graph considered before all edges between cells with different topmost tracks. Now we can contract all those components to single nodes and add the deleted edges thereby obtaining a new graph . For simplicity, number the nodes from 
 to 
 where node 
 represents the component containing the upper left corner.
This graph is clearly bipartite with a bicoloring given by the assignment component  topmost track. Consider the breadth-first tree 
 (starting at node 
). Note that both 
 and 
 can be calculated in time 
.
Proposition 4. The minimum number  of animals needed to create the given pattern equals the depth of 
, i.e. 
.
Proof. We show by induction that the cells visited by the previously described greedy algorithm after using  animals are exactly the cells represented by the nodes within distance 
 from the root. Again the case 
 is trivial. For 
 we can visit all the nodes "in the layer below": since we have 
, the bicoloring of 
 gives a bicoloring of 
 and thus all the nodes in the next layer have the same, namely the currently active, color, and are visited by the greedy algorithm.
Furthermore, the greedy algorithm doesn't reach any other nodes: the nodes visited so far are the previous layers. Assume component  can be visited and take any path from the root to 
 and throw away all the parts up to the last node that is already visited. The remaining nodes have to be of the currently active color, i.e. they belong to the component 
. Thus if 
 hasn't been visited before, it is a child of a node visited before and will be visited in this step. 
This suffices for an  solution (i.e. linear in the size of the input). However, instead of explicitly calculating components, one can assign weights to all the edges in the original graph: any edge between two nodes of the same color gets weight 
 and any other edge weight 
. Then clearly the maximum distance of any node to the root (the upper left corner) equals 
. The distances can be calculated by Dijkstra's algorithm where we can use a 
deque instead of a priority_queue (-Dijkstra) to achieve a linear running time.
Comments