Last active
October 5, 2016 19:05
-
-
Save satyaki3794/43b27e0dd1a25e82fbd2ec823be02c72 to your computer and use it in GitHub Desktop.
DSU bestest code
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 sz[100], par[100]; | |
int root(int v){ | |
if(par[v] == v) return v; | |
return par[v] = root(par[v]); | |
} | |
void unite(int a, int b){ | |
a = root(a), b = root(b); | |
if(a == b) return; | |
if(sz[a] < sz[b]) swap(a, b); | |
par[b] = a; | |
sz[a] += sz[b]; | |
} | |
for(int i=1;i<=100;i++) | |
sz[i]=1, par[i]=i; //initialize DSU |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment