vector<int> parent; vector<int> rank; int find (int x) { if (x == parent[x]) return x; return parent[x] = find(parent[x]); } void Union (int x, int y) { int x_parent = find(x); int y_parent = find(y); if (x_parent == y_parent) return; if(rank[x_parent] > rank[y_parent]) { parent[y_parent] = x_parent; } else if(rank[x_parent] < rank[y_parent]) { parent[x_parent] = y_parent; } else { parent[x_parent] = y_parent; rank[y_parent]++; } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter