Skip to content

Instantly share code, notes, and snippets.

@aakashns
Created June 19, 2012 17:38
Show Gist options
  • Save aakashns/2955478 to your computer and use it in GitHub Desktop.
Save aakashns/2955478 to your computer and use it in GitHub Desktop.
Union Find
#include <iostream>
static const int N = 10000;
int main()
{ int i, j, p, q, id[N], sz[N];
for (i = 0; i < N; i++)
{ id[i] = i; sz[i] = 1; }
while (cin >> p >> q)
{
for (i = p; i != id[i]; i = id[i])
id[i] = id[id[i]];
for (j = q; j != id[j]; j = id[j])
id[j] = id[id[j]];
if (i == j) continue;
if (sz[i] < sz[j])
{ id[i] = j; sz[j] += sz[i]; }
else { id[j] = i; sz[i] += sz[j]; }
cout << " " << p << " " << q << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment