
public class wQuickUnionUF {
	
	private int[] sz, a;
	private int count;
	
	public wQuickUnionUF(int N){
		sz = new int[N];
		for(int i = 0; i < N; i++)
			sz[i] = 1;
		a = new int[N];
		for(int i = 0; i < N; i++)
			a[i] = i;
		count = N;
	}
	
	public boolean connected(int p, int q){
		return find(p) == find(q);
	}
	
	public void union(int p, int q){
		int i = find(p), j = find(q);
		if(i == j) return;
		if(sz[i] > sz[j]){
			a[j] = i;
			sz[i] += sz[j];
		}
		else{
			a[i] = j;
			sz[j] += sz[i];
		}
		count--;
	}
	
	public int find(int p){
		a[p] = a[a[p]];
		while (a[p] != p)
			p = a[p];
		return p;
	}
	
	public int count(){
		return count;
	}
	
	public static void main (String[] args){
		wQuickUnionUF a = new wQuickUnionUF(10);
		a.union(4,6);
		a.union(3,4);
		System.out.println(a.connected(3, 6));
		System.out.println(a.count());
	}

}

@@ -649,24 +649,68 @@
 count;%0A%09%7D%0A%09%0A
+%09//    8-2 4-9 6-3 9-6 0-8 0-1 1-9 4-7 2-5 %0A
 %09public stat
@@ -796,11 +796,43 @@
 ion(
-4,6
+8,2);%0A%09%09a.union(4,9);%0A%09%09a.union(6,3
 );%0A%09
@@ -844,87 +844,172 @@
 ion(
-3,4
+9,6
 );%0A%09%09
-System.out.println(a.connected(3, 6));%0A%09%09System.out.println(a.count());
+a.union(0,8);%0A%09%09a.union(0,1);%0A%09%09a.union(1,9);%0A%09%09a.union(4,7);%0A%09%09a.union(2,5);%0A%09%09%0A%09%09for(int i = 0; i %3C 10; i++)%7B%0A%09%09%09System.out.print(%22%22 + a.a%5Bi%5D + %22 %22);%0A%09%09%7D%0A
 %0A%09%7D%0A

test case