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?
- A. 5
- B. 3
- C. 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
- Problem: A tree on nodes 1–9 has edges 1-2, 2-3, 3-4, 4-5, 5-6, 6-7, 3-8, 8-9. The eccentricity of a node is its largest distance to any…
- Problem: Count the pairs of nodes at the same depth (root depth 0) that are NOT siblings (no common parent), in the tree with parent array…
- Problem: Insert the keys 50, 20, 70, 10, 30, 60, 90, 25, 35, 65 into a binary search tree in this order. How many keys in the tree lie in…
- Problem: Count the ways to segment "catsanddog" into a sequence of words, each taken from the dictionary {cat, cats, and, sand, dog}…
- Problem: How many distinct subsequences (as distinct strings, including the empty string) does "aba" have?
- Problem: A binary string "1100101011" is to be changed so that it alternates (no two adjacent characters are equal). Each operation flips…