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?
- A. 9
- B. 11
- C. 0
- 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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Consider this recursive function: python def f(n): if n <= 1: return f(n - 1) if n % 2 == 0: f(n - 3) How many times is f called in total…
- A counter starts at 1. Each step, you may add 1, add 2, or double the counter. Each step must stay at or below 24. Two sequences of steps…
- Floyd–Warshall runs on vertices 1 to 4 with these arcs u→v (weight): 1→4 (4), 2→1 (1), 2→3 (4), 3→1 (5), 3→4 (10), 4→1 (6), 4→2 (6), 4→3…
- A counter starts at 1. Each step, you may add 1, add 2, or double the counter. Each step must stay at or below 17. Two sequences of steps…
- A grid has 8 rows and 5 columns (rows listed top to bottom; '.' is an open cell, '#' a wall): .#.## ..... #...# .#... ..... ..##. ....#…
- A tree has the vertices D, G, O, Q, T, Y, Z and the edges T–Y, Y–G, Y–D, G–Q, T–Z, Z–O. What is the minimum number of vertices needed so…