Minimum Spanning Tree
O(E log V)
Kruskal: sort edges, add the cheapest that doesn't form a cycle (union-find). Prim: grow from a node via a heap.
0/12 solved
Recognise it
The signal
Connect all points/cities at minimum total cost, or the minimax (bottleneck) edge between two vertices — it lies on the MST.
1Brute force
Try all spanning trees.
2The observation
Exponential choices: the cut property makes the cheapest crossing edge always safe.
3Why it works
Cut property: the edges chosen so far are always a subset of some MST.
Reach for it when
- +minimax edge between two vertices (bottleneck path lies on MST)
Not when
- ×The graph is directeduse Minimum arborescence (a different algorithm)
- ×You want shortest distances from a source (a shortest-path tree is not an MST)use Dijkstra
- ×You think MST minimises each pairwise distance — it only minimises the totaluse Shortest paths for pairwise distances
- ×Only some vertices must be connected (Steiner tree)use Not an MST problem
Practice
- Connecting Cities With Minimum CostFoundationFoundationPremiumMedium
- Road ReparationApply it · Same at CSES scale, report impossible.Apply itCSESMedium
- Min Cost to Connect All PointsOne change · Complete graph on points.One changeGoogle, UberMedium
- Optimize Water Distribution in a VillageTwists · Nodes also have a cost (build a well).TwistsPremiumHard
- Find Critical and Pseudo-Critical Edges in Minimum Spanning TreeMastery checkMastery checkHard
- New Roads QueriesCSESMedium
- Transfer Speeds SumCSESHard
- MST Edge CheckCSESHard
- MST Edge Set CheckCSESHard
- MST Edge CostCSESHard
- Qpwoeirut and VerticesCodeforces2300
- Graph and QueriesCodeforces2600
Pitfalls
Classic bugs
- !Kruskal: sort edges ascending and add an edge only if
find(u) != find(v)— otherwise it forms a cycle. - !Prim: grow from one node with a min-heap of crossing edges.
Go deepervariants and techniques · 3 combinations · resources
Combinations
Minimum Spanning Tree+Union-Find (Disjoint Set)+Greedy
connect all nodes at minimum cost
- Minimum Spanning Tree:
- the goal (spanning tree)
- Union-Find (Disjoint Set):
- cycle check per edge
- Greedy:
- process edges by increasing weight (cut property)
Connecting Cities With Minimum CostRoad ReparationMin Cost to Connect All Points
Kruskal Reconstruction Tree+Binary Lifting & LCA
the minimum possible maximum edge between u and v (or the first time they connect) for many pairs
- Kruskal Reconstruction Tree:
- turns each union into a node valued by the edge weight
- Binary Lifting & LCA:
- LCA queries, or jumping to the highest ancestor with value <= x
Kruskal Reconstruction Tree+Euler Tour / Tree Flattening+Segment Tree / Fenwick (BIT)
edges deleted over time, plus queries for the max value in v's current component
- Kruskal Reconstruction Tree:
- the threshold-reachable set is a subtree
- Euler Tour / Tree Flattening:
- that subtree is a contiguous leaf range
- Segment Tree / Fenwick (BIT):
- range max with point delete over the leaf order