Graph Theory37 sections · 1633 units
Open in Course

Codeforces 295B Greg and Graph - The Reverse Trick

(Process deletions backward)

Instead of deleting nodes, add them back in reverse order. This converts deletions into insertions, which are easier to handle incrementally.

Start with an empty graph. Add nodes in reverse deletion order. After adding each node, compute shortest paths using Floyd-Warshall.

This avoids recomputing everything after every deletion. You build up the graph incrementally instead of tearing it down. Store the sum of all shortest paths at each step, then reverse the answer array.