C++ union find faster than 81%, less than 100%, easy to understand
\nclass Solution {\nprivate:\n    int find(vector<int>& parent, int num) {\n        if (parent[num] != num) {\n            parent[num] = find(parent, parent[num
```\nclass Solution {\nprivate:\n    int find(vector<int>& parent, int num) {\n        if (parent[num] != num) {\n            parent[num] = find(parent, parent[num]);\n        }\n        return parent[num];\n    }\npublic:\n    int makeConnected(int n, vector<vector<int>>& connections) {\n        if (n == 1) {\n            return 0;\n        }\n\n        vector<int> parent(n, 0);\n        iota(parent.begin(), parent.end(), 0);\n        int duplicate = 0;\n        for (vector<int>& connection : connections) {\n            int pu = find(parent, connection[0]);\n            int pv = find(parent, connection[1]);\n            if (pu != pv) {\n                parent[pu] = pv;\n            }\n            else {\n                duplicate += 1;\n            }\n        }\n\n        int components = 0;\n        for (int i = 0; i < n; ++i) {\n            if (i == parent[i]) {\n                components += 1;\n            }\n        }\n\n        return components - 1 > duplicate ? -1 : components - 1;\n    }\n};\n```
[]