Skip to content

Instantly share code, notes, and snippets.

@hsinewu
Created January 20, 2017 17:16
Show Gist options
  • Save hsinewu/f2563eb67a9ccb6e42413b86e07a02b6 to your computer and use it in GitHub Desktop.
Save hsinewu/f2563eb67a9ccb6e42413b86e07a02b6 to your computer and use it in GitHub Desktop.
Data structure
int uf[SIZE];
void init() {
std::iota(uf, uf+SIZE, 0);
}
int root(int x) {
int _x = x;
while(x!=uf[x]) x = uf[x] /* = uf[uf[x]] */ ;
return uf[_x] = x;
}
bool test(int x, int y) {
return root(x) == root(y);
}
void join(int x, int y) {
x = root(x); y = root(y);
uf[min(x,y)] = uf[max(x,y)];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment