public class Code04_UnionFind {
public static class Element<V>{
public V value;
public Element(V value) {
this.value = value;
}
}
public static class UnionFindSet<V>{
public HashMap<V,Element<V>> elementMap;
public HashMap<Element<V>,Element<V>> fatherMap;
public HashMap<Element<V>,Integer> rankMap;
public UnionFindSet(List<V> lists) {
elementMap = new HashMap<>();
fatherMap = new HashMap<>();
rankMap = new HashMap<>();
for (V v : lists) {
Element<V> element = new Element<>(v);
elementMap.put(v, element);
fatherMap.put(element, element);
rankMap.put(element,1);
}
}
public Element<V> findHead(Element<V> element){
Stack<Element<V>> stack = new Stack<>();
while (element != fatherMap.get(element)){
stack.push(element);
element = fatherMap.get(element);
}
while (!stack.isEmpty()){
fatherMap.put(stack.pop(), element);
}
return element;
}
public boolean isSameSet(V a, V b){
if (elementMap.containsKey(a) && elementMap.containsKey(b)){
return findHead(elementMap.get(a)) == findHead(elementMap.get(b));
}
return false;
}
public void union(V a, V b){
if (elementMap.containsKey(a) && elementMap.containsKey(b)){
Element<V> aH = findHead(elementMap.get(a));
Element<V> bH = findHead(elementMap.get(b));
if (aH != bH){
Element<V> big = rankMap.get(aH) >= rankMap.get(bH) ? aH : bH;
Element<V> small = big == aH ? bH : aH;
fatherMap.put(small,big);
rankMap.put(big, rankMap.get(aH) + rankMap.get(bH));
rankMap.remove(small);
}
}
}
}
public static void main(String[] args) {
}
}