Data Structures & AlgorithmsVery hardCodeforces Div 2 · ICPC regional

Directed weighted graph on vertices A, B, C, D with edges B→A (1); B→C (5); C→D (-1); B→D (-1); C→A (-1); D→C (-2); A→D (4). Source A. Run Bellman–Ford for exactly n−1 = 3 passes (each pass tries every edge once). Then, using the distances after those passes and no further updates, how many edges (u→v, w) still satisfy dist[u] + w < dist[v] (with dist[u] finite)?

  1. A. 7
  2. B. 4
  3. C. 1
  4. D. 2

Correct answer: D. 2

After n−1 passes 2 edges still improve, which means a negative cycle is reachable from the source. Checking one pass early, or counting only negative edges, gives different numbers.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic