##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
Union-Find tracks connected components with near-constant time operations. Learn path compression.
Why Union-Find matters
Parent array representation
Merging two components
Flattening during find
Keeping trees balanced
Alternative balancing
The inverse Ackermann function
Classic Union-Find application
Track component count
Detect cycles in graphs
When union fails
Friend circles as components
Union-Find on matrix
Tree = connected + acyclic
Check edges and connectivity
Tracking relationships
Division as graph traversal
Ratios as weights
Implement your solution
Grouping by shared elements
Emails as nodes
Implement your solution
Union by common factors
Factor-based Union-Find
Implement your solution
Union-Find with sorting
Sort cells by elevation
MST with Union-Find
MST on points
Kruskal on complete graph
Undoing union operations
Query ordering matters
Debugging tips
Test your understanding
What you learned
Classic DSU for connected components. Find group sizes after merging users by shared groups.
Transform forest to tree with minimum edge changes. DSU to detect components and count needed edges.
DSU for character equivalence classes. Build smallest string satisfying equality constraints.
MST with degree constraint using DSU. Greedy edge selection with component tracking.
Count connected components on coordinate grid. DSU for points sharing x or y coordinate.
Offline queries with reverse DSU. Process edge deletions as additions in reverse order.
Connect people who share languages. Count additional languages needed for full connectivity.
DSU with rollback for dynamic connectivity. Understand union-find history for queries.
Find components in complement graph. DSU with set for unvisited vertices optimization.
MST queries using DSU and LCA. Find MST weight including specific edge.
Check string transformability with character groups. DSU for equivalent characters.
DSU with parity for bipartite checking. Weighted union-find tracking odd/even distances.
Count connected components in adjacency matrix. Classic DSU introduction problem.
Find cycle-creating edge in tree. First edge where both nodes already in same component.
DSU with string mapping. Merge accounts sharing emails, complex real-world application.
Can be solved with DSU unioning consecutive numbers. Alternative to hash set approach.
Count connected components in grid. DSU alternative to DFS/BFS flood fill.
Stones in same row/column form component. Answer is stones - components.
DSU for equality, then check inequality. Process == first to build components.
Sort characters within each DSU component. Build lexicographically smallest string.