Data Structures & AlgorithmsHardCodeforces Div 2 D · ICPC regional

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

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

Correct answer: A. 3

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