Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS

The directed acyclic graph has the vertices B, C, K, P, Q, R, V, Z and the arcs Z→V, K→R, C→B, K→C, Q→B, Z→B, Z→K, R→V, Q→Z, P→R, K→B, K→V, P→B, R→B, V→B. How many distinct directed paths go from P to B? A path is a sequence of consecutive arcs; a single arc counts as a path.

  1. A. 2
  2. B. 4
  3. C. 5
  4. D. 3

Correct answer: D. 3

Count with ways[x] = sum of ways[y] over arcs y→x, starting at ways[P] = 1 and processing vertices in a topological order. This gives 3 paths; using only paths of at most two arcs would give 2.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic