Disjoint Set Union (union-find)
- Stage
- 5
- Module
- M5.10
- Lessons
- 1
In this module
- Implement find with path halving to track connectivity across components.
- Apply union by size to keep the tree shallow and fast.
- Use the "next free slot" DSU trick to solve resource allocation problems.
- Recognize when DSU contracts components for efficient solutions.
Before this module
Lessons
Practice
Try these on the judge. Each link opens the problem on DMOJ.
- 2015 S3Gates (opens on DMOJ in a new tab) DMOJ
Find the next available gate on a linear arrangement using DSU next-free optimization.
Why DMOJ: A direct application of the next-free-slot trick from this module.
- 2017 S4Minimum Cost Flow (opens on DMOJ in a new tab) DMOJ
Merge components with DSU while breaking ties between equally cheap connections.
Why DMOJ: The full problem builds a minimum spanning tree, a later module's topic, but its DSU merging step is good practice here.