Data Structures & AlgorithmsHardGATE CS · ICPC regional

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

  1. A. 11
  2. B. 12
  3. C. 3
  4. D. 2

Correct answer: A. 11

Count with ways[x] = sum of ways[y] over arcs y→x, starting at ways[I] = 1 and processing vertices in a topological order. This gives 11 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