Data Structures & AlgorithmsHardCodeforces Div 2 D · ICPC regional

A directed acyclic graph has the vertices C, F, K, Q, S, V, W and the directed edges Q→C, V→W, F→C, K→F, V→Q, S→Q, W→F, K→S, K→C, S→C. How many edges does a longest directed path in this graph contain?

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

Correct answer: D. 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 3, and counting vertices instead of edges gives 4.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic