Data Structures & AlgorithmsVery hardCodeforces Div 2 C · ICPC regional

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

  1. A. 161
  2. B. 133
  3. C. 147
  4. D. 5040

Correct answer: B. 133

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic