并查集
并查集是重要的数据结构,在算法编写中很常见。 这里写的比较平实,不使用联合启发式或路径压缩算法。 集合中的元素从0开始编号。 可结合着看:并查集V1.0——森林版。
核心功能- void union(root1, root2) → Merge two sets
- int find(x) → Return set containing x
/**
* Disjoint set class.
* Does not use union heuristics or path compression.
* Elements in the set are numbered starting at 0.
*/
class DisjSetsSlow {
private int[] set;
/