Java simple union find
\nclass Solution {\n    \n    class UnionFind{\n        int[] parent;\n        \n        public UnionFind(int n){\n            parent = new int[n];\n           
```\nclass Solution {\n    \n    class UnionFind{\n        int[] parent;\n        \n        public UnionFind(int n){\n            parent = new int[n];\n            for(int node = 0; node<n; node++){\n                parent[node] = node;\n            }\n        }\n        \n        public void union(int A, int B){\n            int parentA = find(A);\n            int parentB = find(B);\n            parent[parentA] = parentB;\n        }\n        \n        private int find(int A){\n            while(parent[A]!=A){\n                A = parent[A];\n            }\n            return A;\n        }\n    }\n\n    \n    public int makeConnected(int n, int[][] connections) {\n        if(connections.length < n-1)return -1;\n        UnionFind uf = new UnionFind(n);\n        for(int[] conn : connections){\n            uf.union(conn[0],conn[1]);\n        }\n        int notConnectedTerminals = 0;\n        for(int i = 0; i<n;i++){\n            if(i==uf.parent[i])notConnectedTerminals++;\n        }\n        return notConnectedTerminals-1;\n    }\n}\n\n```
['Union Find', 'Java']