Data Structures & AlgorithmsVery hardCodeforces Div 2 C · GATE CS

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

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

Correct answer: C. 3

Count with ways[x] = sum of ways[y] over arcs y→x, starting at ways[J] = 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