Skip to content

Instantly share code, notes, and snippets.

@limabeans
Created November 7, 2017 22:00
Show Gist options
  • Save limabeans/48d9c7652da9b453d5bbec20409f6508 to your computer and use it in GitHub Desktop.
Save limabeans/48d9c7652da9b453d5bbec20409f6508 to your computer and use it in GitHub Desktop.
union find in c++
class UnionFind {
public:
vector<int> parent;
int getParent(int v) {
if (parent[v]==v) return v;
return getParent(parent[v]);
}
void unionn(int v, int w) {
int vp=getParent(v);
int wp=getParent(w);
parent[vp]=wp;
}
UnionFind(int N) {
for (int i=0; i<=N; i++) this->parent.push_back(i);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment