Data Structures & AlgorithmsHardGATE CS · Codeforces Div 2 B

The directed acyclic graph has the vertices E, H, J, K, S, T, V, W and the arcs W→E, S→K, J→H, V→H, J→K, V→W, W→K, E→H, J→T, T→E, V→S, V→E, J→E, W→S. How many distinct directed paths go from V to K? A path is a sequence of consecutive arcs; a single arc counts as a path.

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

Correct answer: B. 3

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