解决动态连通性问题,连接两个点,并且判断两个点是否相通。
public abstract class UF {
protected int[] id;
public UF(int n) {
id = new int[n];
for (int i = 0; i < id.length; i++) {
id[i]=i;
}
}
public boolean connected(int p, int q) {
return find(p) == find(q);
}
public abstract void union(int p, int q);
public abstract int find(int p);
}
package algorithm.repetitiveTraining.uf;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import java.util.Arrays;
import java.util.Date;
import java.util.function.Consumer;
/**
* Foundation project
*
* @author FlagDied
* @date 2020/2/17
*/
public class WeightedQuickUnion extends UF {
protected int[] sz;
public WeightedQuickUnion(