Simple Java BFS, just count num of Islands
\n    public int makeConnected(int n, int[][] cs) {\n        int m = cs.length;\n        if (m < n -1) return -1;\n        if (m < 2) return 0;\n        Map<Int
```\n    public int makeConnected(int n, int[][] cs) {\n        int m = cs.length;\n        if (m < n -1) return -1;\n        if (m < 2) return 0;\n        Map<Integer, Set<Integer>> mp = new HashMap<>();\n        for (int[] c : cs){\n            Set<Integer> sa = mp.getOrDefault(c[0], new HashSet<Integer>());\n            sa.add(c[1]);\n            mp.put(c[0], sa);\n            Set<Integer> sb = mp.getOrDefault(c[1], new HashSet<Integer>());\n            sb.add(c[0]);\n            mp.put(c[1], sb);        \n        }\n        \n        int is = 0;\n        Set<Integer> vs = new HashSet<>();\n        for (int i = 0; i < n; i++){\n            if(!vs.contains(i)){\n                is++;\n                vs.add(i);\n                bfs(i, vs, mp);\n            }\n        }\n        return is - 1;\n    }\n    \n    private void bfs(int i, Set<Integer> vs, Map<Integer, Set<Integer>> mp){\n        Queue<Integer> q = new LinkedList<Integer>();\n        q.offer(i);\n        while(!q.isEmpty()) {\n            int curr = q.poll();\n            Set<Integer> st = mp.getOrDefault(curr, new HashSet<>());\n            for (int neig : st){\n                if (!vs.contains(neig)){\n                    q.offer(neig);\n                    vs.add(neig);\n                }\n            }\n        }\n    }\n\t```
[]