[JAVA] union find, 99%time 100%space with explanation
use union find to get the num of connected networks.\n2. check whether these 2 computers are already connected or not when do the union, then u can get the num 
1. use union find to get the num of connected networks.\n2. check whether these 2 computers are already connected or not when do the union, then u can get the num of extra cables.\n3. need `n-1` cables to connect `n` different networks, so compare the `extra cables`  & `num of networks - 1`.\n```\nclass Solution {\n    public int makeConnected(int n, int[][] connections) {\n        int[] c = new int[n];\n        int group = 0, extra = 0;\n        for(int i = 0; i < n; i++) c[i] = i;\n        for(int[] a : connections)\n            if(union(a[0], a[1], c))\n                extra ++;\n        for(int i = 0; i < n; i++) {\n            if(find(i, c) == i)\n                group ++;\n        }\n        return extra >= group - 1 ? group - 1 : -1;\n    }\n    \n    boolean union(int a, int b, int[] c) {\n        if(find(a, c) != find(b, c)){\n            c[c[a]] = c[b];\n            return false;\n        }\n        else return true;\n    }\n    \n    int find(int a, int[] c) {\n        if(c[a] != a) \n            c[a] = find(c[a], c);\n        return c[a];\n    }\n}\n```
['Union Find', 'Java']