Created
January 20, 2017 17:16
-
-
Save hsinewu/f2563eb67a9ccb6e42413b86e07a02b6 to your computer and use it in GitHub Desktop.
Data structure
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
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