rank
: void mkroot(int newroot, int prevroot) {
for (int child : children[prevroot] {
parent[child] = newroot;
}
parent[prevroot] = newroot];
children[prevroot] = {}; // this has no children anymore
}
prevroot
and the larger subtree the newroot
: It is better to loop over fewer children. rank
based union, we are using the same heuristic , even though we don't actually loop over all our children.