Algorithm – Union Find
public class UnionFind {
static class QuickFindUF {
// id[i] is
int[] id;
/**
* Initialize data structure
*/
QuickFindUF(int N) {
id = new int[N];
for (int i = 0; i < N; i++) {
id[i] = i;
}
}
/**
* p and q are connected if they have the same id. Time complexity is O(1)
*/
public boolean connected(int p, int q) {
return id[p] == id[q];
}
/**
* To merge components containing p and q, change all entries whose id equals
共有 0 条评论