Objective
The union-find structure keeps disjoint groups: each element points to a “parent” and the root identifies the group. Merging two groups and knowing whether two elements are together are almost instantaneous operations.
Instructions
- The first line has
n m(elements, from 0 to n-1, and number of unions); thenmlinesa b, each merging the group ofawith that ofb. - After each union show, on its own line, how many distinct groups remain (at the start there are
n).
5 4
0 1
2 3
1 3
0 3
4
3
2
2
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise