Data Structures & AlgorithmsModerateCodeforces Div 2 C · GATE CS

An undirected graph has the vertices D, E, H, J, M, P and the edges P–E, P–H, P–J, E–H, E–D, M–J, M–D. In how many ways can each vertex be given one of 3 colours (colours are labelled, and no two adjacent vertices may share a colour)?

  1. A. 60
  2. B. 30
  3. C. 729
  4. D. 96

Correct answer: B. 30

Count the proper 3-colourings by backtracking over the vertices; there are 30. Forgetting the edges gives 729, and subtracting each edge's monochromatic colourings once (no overlaps) gives -972.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic