Data Structures & AlgorithmsVery hardCodeforces Div 2 D · ICPC regional

A directed acyclic graph has the vertices D, G, K, L, Q, S, V, Y and the directed edges D→K, D→Q, D→L, D→V, Q→L, G→L, D→Y, Y→Q, K→Y, K→V, S→V, K→L, D→S, G→Y, V→Q. How many edges does a longest directed path in this graph contain?

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

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic