class DisjointSet {
    private final int[] father;
    private final int[] rank;
    private final int N;

    DisjointSet(int N){
        this.N = N;
        rank = new int[N + 1];
        father = new int[N + 1];

        for (int i = 1; i <= N; i++)
            initialize(i);
    }

    void initialize(int node){
        if (!(1 <= node && node <= N)) throw new AssertionError();
        father[node] = node;
        rank[node] = 1;
    }

    int find(int node){
        if (!(1 <= node && node <= N)) throw new AssertionError();

        if (father[node] == node)
            return node;
        else
            return father[node] = find(father[node]);
    }

    void union(int x, int y){
        x = find(x);
        y = find(y);

        if (x != y){
            if (rank[x] < rank[y])
                father[x] = y;
            else
                father[y] = x;

            if (rank[x] == rank[y])
                rank[x]++;
        }
    }

    boolean connected(int x, int y){
        return find(x) == find(y);
    }
}

@@ -99,16 +99,52 @@
 l int N;
+%0A    private int numberOfComponents;
 %0A%0A    Di
@@ -309,16 +309,49 @@
 lize(i);
+%0A%0A        numberOfComponents = N;
 %0A    %7D%0A%0A
@@ -823,24 +823,59 @@
 f (x != y)%7B%0A
+            numberOfComponents--;%0A%0A
             
@@ -1086,32 +1086,32 @@
 (int x, int y)%7B%0A
-
         return f
@@ -1134,11 +1134,86 @@
 );%0A    %7D
+%0A%0A    int getNumberOfComponents()%7B%0A        return numberOfComponents;%0A    %7D
 %0A%7D%0A

Update DisjointSet.java