Editorial for DMOPC '20 Contest 6 P3 - Bread Merchant
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
We model the cities and routes as a digraph (directed graph). For this subtask, the graph has the special structure constraint
The standard way to approach game theory problems on a DAG is by dynamic programming. To do this, we need to iterate down from
- We always mark nodes with no outgoing edges.
- If
, we mark node if all outgoing edges lead to marked nodes. - If
, we mark node if at least one outgoing edge leads to a marked node.
The time complexity is
Subtask 2
For this subtask, the robber always chooses which edge to take. Then the robber will be caught if all paths eventually lead to a node with no outgoing edges, and the robber will escape if there is a path leading to a cycle.
We can efficiently find which nodes are which using the standard algorithm for topological sort. At a high level, the algorithm deletes nodes that have no outgoing edges, and edges that go to such nodes. The edge deletions cause new nodes to have no outgoing edges, so they too are deleted. The algorithm repeats this until all remaining nodes have at least one outgoing edge. Then starting at any of the remaining nodes, there will always be an edge to take that goes to another of the remaining nodes, thereby allowing the robber to escape forever.
The implementation is the same as is typical for topological sort. We maintain the current out-degrees of all nodes, a set (e.g. a queue) of nodes we are processing. The algorithm uses a "reverse adjacency list" (i.e. an adjacency list of the digraph with edges reversed) to efficiently update the out-degrees.
The time complexity is
Subtask 3
For this subtask, the police always choose which edge to take. So the robber will be caught if and only if a node with no outgoing edges is reachable by any path.
Call the nodes with no outgoing edges the set
The time complexity is
Subtask 4
Like in Subtask 1, we will aim to mark exactly the nodes where the robber will eventually get caught. We will use the same three rules:
- We mark nodes with no outgoing edges.
- If
, we mark node if all outgoing edges lead to marked nodes. - If
, we mark node if at least one outgoing edge leads to a marked node.
Our algorithm will be to proceed in phases. In each phase, we look at each unmarked node and see if we can apply the rule to mark the node. We stop when a phase makes no changes. Then since a node can be marked only once, there are at most
Starting from the unmarked nodes, the robber will be able to escape. This is because for every unmarked node, there is at least one outgoing edge, and the outgoing edge chosen will always go to another unmarked node (whether or not
Depending on the implementation, the time complexity will be something like
Subtask 5
The full solution combines the ideas of Subtasks 2, 3, and 4. That is, the idea of marking nodes, and the proof of correctness, is the same as Subtask 4. However, the implementation is very similar to a combination of topological sort and a graph search, seen in Subtasks 2 and 3.
The implementation first builds an adjacency list of the reverse digraph. It also maintains the marked nodes, the set of nodes yet to be processed (e.g. in a queue), and the current out-degrees of the unmarked nodes. We begin by marking all nodes with no outgoing edges. Then when processing any marked node, we examine all nodes
If properly implemented, the time complexity is
Notes
This problem shows that topological sort and typical graph searches (e.g. BFS and DFS) are both special cases of a broader type of algorithm. The algorithm has a specific value
This problem is an easier version of problem H on the 2018 BAPC contest. That problem has weighted edges, and requires a generalization of Dijkstra's algorithm (like how this algorithm generalizes BFS).
If you want a harder game theory problem on a graph, check out Toy Train. By solving this problem, you have a good foundation to approach it.
Comments