@@ -0,0 +1,607 @@
+package com.janosgyerik.tools.algorithm.unionfind;%0A%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0Apublic class UnionFind %7B%0A  private final Map%3CInteger, Integer%3E parents = new HashMap%3C%3E();%0A%0A  public void union(int u, int v) %7B%0A    int pu = find(u);%0A    int pv = find(v);%0A    if (pu == pv) return;%0A    parents.put(pu, pv);%0A  %7D%0A%0A  public int find(int u) %7B%0A    if (!parents.containsKey(u)) %7B%0A      parents.put(u, u);%0A      return u;%0A    %7D%0A%0A    int pu = parents.get(u);%0A    while (u != pu) %7B%0A      int ppu = parents.get(pu);%0A      parents.put(u, ppu);%0A      u = pu;%0A      pu = ppu;%0A    %7D%0A%0A    return pu;%0A  %7D%0A%7D%0A

Add UnionFind