Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS

A directed acyclic graph has the vertices F, N, R, S, T, U, V, X and the directed edges R→V, U→S, S→X, F→X, N→V, R→S, U→F, F→N, R→F, R→T, S→F. How many edges does a longest directed path in this graph contain?

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

Correct answer: D. 4

Take dp[x] = 1 + max(dp[y]) over arcs y→x, with dp = 0 for sources; the largest dp value is 4 edges. Following the first listed out-arc at each step gives only 2, and counting vertices instead of edges gives 5.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic