Created
February 3, 2020 04:28
-
-
Save andriybuday/9fa907eaaba8431ae633af57ecc2e66c to your computer and use it in GitHub Desktop.
DisjointSetUnion
This file contains hidden or 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
class DisjointSetUnion { | |
int[] parent; | |
public DisjointSetUnion(int n) { | |
parent = new int[n]; | |
for(int i = 0; i < n; ++i) { | |
parent[i] = i; | |
} | |
} | |
public int find(int x) { | |
if(parent[x] != x) { | |
parent[x] = find(parent[x]); | |
} | |
return parent[x]; | |
} | |
public void union(int x, int y) { | |
parent[find(x)] = find(y); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment