Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2 B

A directed acyclic graph has the vertices K, L, P, U, W, X, Y and the directed edges Y→W, Y→K, K→L, K→X, P→K, U→X, W→X, W→P, U→P, U→K, W→L, W→K, U→W. How many edges does a longest directed path in this graph contain?

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

Correct answer: B. 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