Data Structures & AlgorithmsVery hardCodeforces Div 2 C · GATE CS

An undirected connected graph on the vertices B, M, Q, R, U has the edges B–U, M–R, Q–R, M–Q, B–R, B–M, Q–U, B–Q. How many spanning trees does it have? (A spanning tree is a set of edges that connects all vertices with no cycle.)

  1. A. 4
  2. B. 16
  3. C. 70
  4. D. 40

Correct answer: D. 40

The Matrix-Tree theorem gives the count as any cofactor of the Laplacian, here 40. Choosing any 4 of the 8 edges gives 70 sets, but most of them contain a cycle or leave a vertex out.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic