PrepStack · Markdown HTML

Offline activation with DSU

DSU, MST, and offline activation · HTML

Formal shape

Vertices or edges carry keys, and a query depends only on comparing a key with threshold t; sort events and queries so the active set grows one way.

Recognition signals

False friends

SignalWhy it is different
Answers must follow real-time arrivalOffline sorting changes processing order; a truly online problem needs another structure.
State both enters and leavesPlain DSU handles monotone activation; non-monotone changes need rollback or divide-and-conquer machinery.

Core invariant

Before answering threshold t, exactly the qualifying events are active, and DSU components equal the true components under t.

State, transition, and processing order

ItemDefinition
StateSorted events, indexed queries, DSU, a scan pointer, and active flags in a vertex-activation model.
TransitionAdvance the threshold, activate and union every newly eligible event, then read the query and store it at its original index.
Frontier / orderEverything left of the scan pointer is active, everything right is inactive, and the pointer never retreats.

Python skeleton

def offline_connectivity(n, edges, queries):
    p = list(range(n))
    def find(x):
        while x != p[x]:
            p[x] = p[p[x]]; x = p[x]
        return x
    def union(a, b):
        a, b = find(a), find(b)
        if a != b: p[b] = a
    edges = sorted(edges)  # (weight,u,v)
    order = sorted((lim, i, u, v) for i, (lim,u,v) in enumerate(queries))
    ans, j = [False] * len(queries), 0
    for lim, i, u, v in order:
        while j < len(edges) and edges[j][0] <= lim:
            _, a, b = edges[j]; union(a, b); j += 1
        ans[i] = find(u) == find(v)
    return ans

Correctness sketch

  1. Sorting places all and only events with key<=limit left of the pointer.
  2. Unioning each active edge makes DSU components match the active subgraph.
  3. A query only reads that state, so it sees exactly its threshold-defined graph.
  4. Writing by original index restores the requested output order.

Complexity

TimeSpaceParameters
O((E+Q) log(E+Q)+(E+Q)α(V))O(V+E+Q)V objects, E activation events, Q queries

Edge cases

Error patterns

PatternWhy it fails / fix
Wrong equal-key tie breakFor <=, same-key events must precede the query.
Appending in sorted orderStore answers by original query index.
Rebuilding DSU per queryThat discards the shared-scan advantage.

Selection and exclusion rules

RelationTemplateBoundary
dynamic summaryUnion-Find dynamic connectivityComponents must be maintained after activation

Original micro-example

PartDetail
ResultThreshold 4 contains only the first edge; threshold 5 inserts both tied edges before answering.
ScenarioEdges activate at thresholds 2, 5, and 5; connectivity is queried at 4 and 5.
WalkthroughThe pointer advances once and every edge is processed once, so both queries reuse prior work.

Recall prompts

My notes