Data Structures & AlgorithmsHardCodeforces Div 2 D · ICPC regional

A directed acyclic graph has the vertices A, D, M, O, Q, U, V and the edges U→M, U→D, U→V, Q→U, Q→O, A→M, A→V, O→U. A topological order lists every vertex once so that each edge goes from an earlier to a later vertex. Which topological order is lexicographically smallest when read as a sequence of letters?

  1. A. Q O U D A M V
  2. B. A Q O U D M V
  3. C. Q O U D A V M
  4. D. Q O U A M V D

Correct answer: B. A Q O U D M V

Kahn's algorithm that always takes the smallest available vertex (a min-heap) gives A Q O U D M V. A FIFO queue gives A Q O U D M V, which can differ, and a reverse-postorder DFS gives Q O U D A V M.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic