[Java] Union Find (count components and extra edges)
```\nclass Solution {\n    public static int findParent(int[] par, int i) {\n        if(par[i] == i) return i;\n        return par[i] = findParent(par, par[i]);
```\nclass Solution {\n    public static int findParent(int[] par, int i) {\n        if(par[i] == i) return i;\n        return par[i] = findParent(par, par[i]);\n    }\n    public int makeConnected(int n, int[][] connections) {\n        int[] parent = new int[n];\n        for(int i = 0; i < n; i++) parent[i] = i;\n        int m = connections.length;\n        int components = 0;\n        int extraEdge = 0;\n        for(int i = 0; i < m; i++) {\n            int p1 = findParent(parent, connections[i][0]);\n            int p2 = findParent(parent, connections[i][1]);\n            if(p1 == p2) extraEdge++;\n            else parent[p1] = p2;\n        }\n        for(int i = 0; i < n; i++) if(parent[i] == i) components++;\n        return (extraEdge >= components - 1) ? components - 1 : -1;\n    }\n}
[]