Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS

An undirected graph has the vertices A, G, Q, Z and the edges Z–A, Z–G, Q–A, Q–G. 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. 18
  2. B. 24
  3. C. 36
  4. D. 81

Correct answer: A. 18

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic