并查集复习
来自weiwei大佬的代码:
https://leetcode-cn.com/problems/satisfiability-of-equality-equations/solution/shi-yong-bing-cha-ji-chu-li-bu-xiang-jiao-ji-he-we/
class UnionFind {
private int[] parent;
public UnionFind(int len) {
parent = new int[len];
for (int i = 0; i < len; i++) {
parent[i] = i;
}
}
public int find(int x) {
while (x != parent[x]) {
parent[x] = parent[parent[x]];
x = parent[x];
}
return x;
}
public void union(int x,int y) {
int rootX = find(x);
int rootY = find(y);
parent[rootX] = rootY;
}
public boolean isConnect(int x,int y) {
return find(x) == find(y);
}
}