import java.util.Arrays;

public class UnionFind {

	private static Node[] set;

	private static void union(Node x, Node y) {
		Node x_root = find(x);
		Node y_root = find(y);
		if (x_root == y_root)
			return;

		// x and y are not already in same set. Merge them.
		if (x_root.rank < y_root.rank)
			x_root.parent = y_root;
		else if (x_root.rank > y_root.rank)
			y_root.parent = x_root;
		else {
			y_root.parent = x_root;
			x_root.rank++;
		}
	}

	private static Node find(Node x) {
		if (x.parent != x)
			x.parent = find(x.parent);
		return x.parent;
	}

	public static void main(String[] args) {
		final int n = 10;

		set = new Node[n];
		for (int i = 0; i < n; i++)
			set[i] = new Node(i);

		int[][] pairs = { { 1, 2 }, { 3, 1 }, { 3, 5 }, { 4, 6 }, { 5, 4 },
				{ 4, 3 }, { 5, 2 }, { 2, 1 }, { 7, 10 }, { 9, 10 }, { 8, 9 } };
		for (int i = 0; i < pairs.length; i++) {
			int a = pairs[i][0] - 1;
			int b = pairs[i][1] - 1;

			union(set[a], set[b]);
		}
		
		System.out.println(Arrays.toString(set));
	}

	private static class Node {
		private Node parent;
		private int rank, index;

		private Node(int index) {
			rank = 0;
			this.index = index;
			parent = this;
		}

		public String toString() {
			Node n = this;
			while (n.index != n.parent.index)
				n = n.parent;
			return "" + n.index;
		}
	}

}

@@ -1,30 +1,4 @@
-import java.util.Arrays;%0A%0A
 publ
@@ -35,20 +35,19 @@
  static 
-Node
+int
 %5B%5D set;%0A
@@ -78,72 +78,46 @@
 ion(
-Node x, Node y) %7B%0A%09%09Node x_root = find(x);%0A%09%09Node y_root
+int i, int j) %7B%0A%09%09set%5Bfind(i)%5D
  = find(
 y);%0A
@@ -116,393 +116,181 @@
 ind(
-y
+j
 );%0A%09
-%09if (x_root == y_root)%0A%09%09%09return;%0A%0A%09%09// x and y are not already in same set. Merge them.%0A%09%09if (x_root.rank %3C y_root.rank)%0A%09%09%09x_root.parent = y_root;%0A%09%09else if (x_root.rank %3E y_root.rank)%0A%09%09%09y_root.parent = x_root;%0A%09%09else %7B%0A%09%09%09y_root.parent = x_root;%0A%09%09%09x_root.rank++;%0A%09%09%7D%0A%09%7D%0A%0A%09private static Node find(Node x) %7B%0A%09%09if (x.parent != x)%0A%09%09%09x.parent = find(x.parent);%0A%09%09return x.parent
+%7D%0A%0A%09private static int find(int i) %7B%0A%09%09return set%5Bi%5D == i ? i : (set%5Bi%5D = find(set%5Bi%5D));%0A%09%7D%0A%0A%09private static boolean sameSet(int i, int j) %7B%0A%09%09return find(i) == find(j)
 ;%0A%09%7D
@@ -366,20 +366,19 @@
 t = new 
-Node
+int
 %5Bn%5D;%0A%09%09f
@@ -420,19 +420,9 @@
 %5D = 
-new Node(i)
+i
 ;%0A%0A%09
@@ -692,355 +692,98 @@
 %09%09%7D%0A
-%09%09
 %0A%09%09
-System.out.println(Arrays.toString(set));%0A%09%7D%0A%0A%09private static class Node %7B%0A%09%09private Node parent;%0A%09%09private int rank, index;%0A%0A%09%09private Node(int index) %7B%0A%09%09%09rank = 0;%0A%09%09%09this.index = index;%0A%09%09%09parent = this;%0A%09%09%7D%0A%0A%09%09public String toString() %7B%0A%09%09%09Node n = this;%0A%09%09%09while (n.index != n.parent.index)%0A%09%09%09%09n = n.parent;%0A%09%09%09return %22%22 + n.index;%0A%09%09%7D
+for (int i = 0; i %3C n; i++)%0A%09%09%09System.out.print(find(i) + %22 %22);%0A%09%09System.out.println();
 %0A%09%7D%0A

Update UnionFind.java