Skip to content

Instantly share code, notes, and snippets.

@satyaki3794
Last active October 5, 2016 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satyaki3794/43b27e0dd1a25e82fbd2ec823be02c72 to your computer and use it in GitHub Desktop.
Save satyaki3794/43b27e0dd1a25e82fbd2ec823be02c72 to your computer and use it in GitHub Desktop.
DSU bestest code
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