Created
November 7, 2017 22:00
-
-
Save limabeans/48d9c7652da9b453d5bbec20409f6508 to your computer and use it in GitHub Desktop.
union find in c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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