Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS

A directed acyclic graph has the vertices I, K, O, Q, T, U, X, Z and the directed edges K→Z, U→K, U→Z, T→K, X→I, X→K, O→K, T→I, X→U, X→O, X→T. How many edges does a longest directed path in this graph contain?

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

Correct answer: B. 3

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic