Data Structures & AlgorithmsModerateCodeforces Div 2 C · ICPC regional

A directed acyclic graph on vertices 0 to 4 has exactly these edges (u→v means u must come before v): 0→2, 1→0, 3→2, 4→0, 4→2. How many linear orders of all 5 vertices respect every edge (topological orderings)?

  1. A. 16
  2. B. 15
  3. C. 8
  4. D. 120

Correct answer: C. 8

Counting orders with a subset DP gives 8. Dropping one edge from the constraints gives 16, and n! = 120 ignores all constraints; both are the slips.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic