Graph Theory37 sections · 1633 units
Open in Course

Game Routes - Algorithm

Topological order guarantees

1.1. Run Kahn's algorithm to get the topological order of the levels.

2.2. Initialize ways[1] = 1 and all other ways[i] = 0.

3.3. Process levels in topological order.

For each level uu: For each neighbor vv where edge uvu \to v exists: ways[v] = (ways[v] + ways[u]) % 10^9 + 7.

4.4. After processing all levels, the answer is ways[n].

Why modulo? Because the number of paths can be exponentially large. The problem asks for the result modulo 109+710^9 + 7 to keep numbers manageable.

This runs in O(V+E)O(V + E) time and uses O(V)O(V) space.