Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS

A directed acyclic graph has the vertices D, F, I, P, R, X, Y and the directed edges I→F, Y→F, R→P, X→P, Y→D, D→P, X→D, R→F, Y→X. How many edges does a longest directed path in this graph contain?

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

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 1, and counting vertices instead of edges gives 4.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic