Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div2 D

Problem: In the undirected graph with edges 0-1, 0-2, 1-3, 2-3, 3-4, 4-5, what is the length of the shortest walk from 0 to 5 that uses an even number of edges?

  1. A. 5
  2. B. 3
  3. C. 4
  4. D. 6

Correct answer: C. 4

Run BFS on the state (vertex, parity of edges used so far) so that walks may revisit vertices. Plain BFS would give the shortest walk regardless of parity, which can be odd.

More Coding Interview Problems questions

Exams that ask this topic