class UnionFind { public: vector<int> parent,count; UnionFind(int n){ parent.resize(n, -1); count.resize(n, 1); } int find(int x){ return (this->parent[x] == -1)? x : find(this->parent[x]); } void Union(int a, int b){ int pA = find(a) , pB = find(b); if(pA != pB){ this->parent[pB] = pA; this->count[pA] += this->count[pB]; } } };
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