Technique · CP 2400+
DSU with Rollback / Offline Dynamic Connectivity
Union by size with no path compression, so each union can be undone from a stack. Put each edge's lifetime on a segment tree over time and DFS that tree, uniting on entry and rolling back on exit. This answers connectivity under insertions and deletions offline in O(q log q log n).
0/5 solved
Recognise it
The signal
edges are added AND removed, interleaved with connectivity/component/bipartiteness queries
1Brute force
Rebuild the DSU or BFS after every change: O(q(n + m)).
2The observation
A DSU cannot delete.
3Why it works
At each segment-tree node the DSU holds exactly the edges alive throughout that node's time interval, and rollback restores the parent array exactly.
Reach for it when
- +all queries known in advance
Not when
- ×insertions only (plain DSU)
- ×deletions only (reverse time: offline-queries)
- ×forced-online queries
Practice
- Dynamic ConnectivityCSESHard
- Extending Set of PointsCodeforces2600
- EnvyCodeforces2300
- Bipartite CheckingCodeforces2500
- Forced Online Queries ProblemCodeforces2600
Pitfalls
Misconceptions
- !path compression breaks rollback
- !an edge alive over [l, r) goes into O(log q) nodes, not one
Go deeper1 combinations · resources
Combinations
DSU with Rollback / Offline Dynamic Connectivity+Segment Tree / Fenwick (BIT)+Offline Query Processing
add edge / remove edge / count components (or check bipartiteness) with all operations given upfront
- DSU with Rollback / Offline Dynamic Connectivity:
- unite on entry, roll back on exit
- Segment Tree / Fenwick (BIT):
- a segment tree over time storing each edge in the O(log q) nodes covering its lifetime
- Offline Query Processing:
- every add/remove/query must be known before processing
Dynamic ConnectivityExtending Set of PointsBipartite Checking