Easy Java DFS method
```\nclass Solution {\n    public int makeConnected(int n, int[][] connections) {\n        if(n>connections.length+1)\n            return -1;\n        Map> grap
```\nclass Solution {\n    public int makeConnected(int n, int[][] connections) {\n        if(n>connections.length+1)\n            return -1;\n        Map<Integer,List<Integer>> graph=new HashMap<>();\n        for(int i=0;i<n;i++)\n            graph.put(i,new ArrayList<Integer>());\n        for(int[] row:connections){\n            graph.get(row[0]).add(row[1]);\n            graph.get(row[1]).add(row[0]);\n        }\n        int ans=0;\n        boolean[] visited=new boolean[n];\n        for(int i=0;i<n;i++){\n            if(!visited[i]){\n                dfsUtil(i,graph,visited);\n                ans++;\n            }\n        }\n        return ans-1;\n    }\n    public void dfsUtil(int i,Map<Integer,List<Integer>> graph,boolean[] visited){\n        visited[i]=true;\n        for(int nei:graph.get(i)){\n            if(!visited[nei])\n                dfsUtil(nei,graph,visited);\n        }\n    }\n}\n
[]