Continue learning
Practice related DSA questions in the same topic.
Connect stations using disjoint sets and union by size.
For each link, find both representatives and choose the larger root. If sizes tie, choose the first endpoint’s root.
| 0 | 1 | 2 | 3 | 4 |
| 0 | 1 | 2 | 3 | 4 |
Step 0 · Connect all stations with union by size.
Union by size attaches the smaller tree to the larger. Without path compression, root lookup is O(log n).
Play this game