@@ -0,0 +1,795 @@
+package org.pilgrim.leetcode.y2022;%0A%0Aimport java.util.Arrays;%0Aimport java.util.Iterator;%0A%0Apublic class UnionFind %7B%0A%0A%09public static void main(String%5B%5D args) %7B%0A%09%09UnionFind uf = new UnionFind(10);%0A%09%09uf.union(1, 5);%0A%09%09uf.union(1, 3);%0A%09%09uf.union(3, 2);%0A%09%09uf.union(3, 0);%0A%09%09uf.union(8, 0);%0A%09%09%0A%09%09uf.union(6, 7);%0A%09%09%0A%09%09System.out.println(Arrays.toString(uf.parents));%0A%09%09%0A%09%09for (int i = 0; i %3C 10; i++) %7B%0A%09%09%09System.out.println(uf.find(i));%0A%0A%09%09%7D%0A%09%09%0A%09%09System.out.println(Arrays.toString(uf.parents));%0A%0A%0A%09%7D%0A%09%0A%09int%5B%5D parents;%0A%09%0A%09public UnionFind(int n) %7B%0A%09%09parents = new int%5Bn%5D;%0A%09%09%0A%09%09for(int i=0; i%3Cn;i++) %7B%0A%09%09%09parents%5Bi%5D = i;%0A%09%09%7D%0A%09%7D%0A%09%0A%09public void union(int a, int b) %7B%0A%09%09parents%5Bfind(b)%5D = find(a);%0A%09%7D%0A%09%0A%09public int find(int a) %7B%0A%09%09if(parents%5Ba%5D == a) %7B%0A%09%09%09return a;%0A%09%09%7D%0A%09%09%0A%09%09return find(parents%5Ba%5D);%0A%09%7D%0A%0A%7D%0A

add union find