Data Structures & AlgorithmsVery hardCodeforces Div 2 D · ICPC regional

An undirected graph has the vertices A, B, J, Q, Y and the edges Q–J, Q–B, Y–J, Y–B, A–J. A walk may repeat vertices and edges. How many walks of length exactly 4 go from Q to Y?

  1. A. 9
  2. B. 11
  3. C. 0
  4. D. 23

Correct answer: A. 9

The (i, j) entry of the k-th power of the adjacency matrix counts walks of length k, giving 9. Simple paths from Q to Y give only 2, and walks of length 3 give 0.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic