Commutable Islands

There are A islands and there are M bridges connecting them. Each bridge has some cost attached to it.

We need to find bridges with minimal cost such that all islands are connected.

It is guaranteed that input data will contain at least one possible scenario in which all islands are connected with each other.

Input Format:

The first argument contains an integer, A, representing the number of islands.
The second argument contains an 2-d integer matrix, B, of size M x 3:
    => Island B[i][0] and B[i][1] are connected using a bridge of cost B[i][2].

Output Format:

Return an integer representing the minimal cost required.

Constraints:

1 <= A, M <= 6e4
1 <= B[i][0], B[i][1] <= A
1 <= B[i][2] <= 1e3

Examples:

Input 1:
    A = 4
    B = [   [1, 2, 1]
            [2, 3, 4]
            [1, 4, 3]
            [4, 3, 2]
            [1, 3, 10]  ]

Output 1:
    6

Explanation 1:
    We can choose bridges (1, 2, 1), (1, 4, 3) and (4, 3, 2), where the total cost incurred will be (1 + 3 + 2) = 6.

Input 2:
    A = 4
    B = [   [1, 2, 1]
            [2, 3, 2]
            [3, 4, 4]
            [1, 4, 3]   ]

Output 2:
    6

Explanation 2:
    We can choose bridges (1, 2, 1), (2, 3, 2) and (1, 4, 3), where the total cost incurred
Solution:

Kruskal's algo

public class Solution {
    static class UF {
        private int count;
        private int[] parent;
        private int[] size;
        
        public UF(int n) {
            this.count = n;
            parent = new int[n];
            size = new int[n];
            for (int i = 0; i < n; i ++) {
                parent[i] = i;
                size[i] = 1;
            }
        }
        
        public int find(int i) {
            while (parent[i] != i) {
                parent[i] = parent[parent[i]];
                i = parent[i];
            }
            return i;
        }
        
        public void union(int x, int y) {
            int rootX = find(x);
            int rootY = find(y);
            if (rootX == rootY) return;
            if (size[rootX] <= size[rootY]) {
                parent[rootX] = rootY;
                size[rootY] += size[rootX];
            } else {
                parent[rootY] = rootX;
                size[rootX] += size[rootY];
            }
            count --;
        }
        
        public boolean connected(int x, int y) {
            int rootX = find(x);
            int rootY = find(y);
            return rootX == rootY;
        }
        
        public int count() {
            return count;
        }
    }
    
    static class Edge implements Comparable<Edge> {
        int x;
        int y;
        int cost;
        
        public Edge(int x, int y, int cost) {
            this.x = x;
            this.y = y;
            this.cost = cost;
        }
        
        public int compareTo(Edge e) {
            return Integer.compare(this.cost, e.cost);
        }
    }
    
    public int solve(int A, int[][] B) {
        int m = B.length;
        Edge[] edges = new Edge[m];
        UF bridges = new UF(A + 1);
        for (int i = 0; i < m; i ++) {
            int[] bridge = B[i];
            edges[i] = new Edge(bridge[0], bridge[1], bridge[2]);
        }
        Arrays.sort(edges);
        int cost = 0;
        for (Edge e : edges) {
            int x = e.x;
            int y = e.y;
            if (!bridges.connected(x, y)) {
                bridges.union(x, y);
                cost += e.cost;
            }
        }
        return cost;
    }
}