C++ | Union Find | Connected components
\nclass Solution {\n    vector<int>parent;\n    int find(int x) {\n        return (parent[x] == x) ? x : find(parent[x]);\n    }\npublic:\n    int makeConnected
```\nclass Solution {\n    vector<int>parent;\n    int find(int x) {\n        return (parent[x] == x) ? x : find(parent[x]);\n    }\npublic:\n    int makeConnected(int n, vector<vector<int>>& connections) {\n        \n        int c = connections.size();\n        if (n-1 > c) return -1;\n        \n        parent.resize(n);\n        for (int i = 0; i < n; i++)\n            parent[i] = i;\n        \n        for (int i = 0; i < c; i++) {\n            int x = find(connections[i][0]);\n            int y = find(connections[i][1]);\n            if (x != y) parent[y] = x;\n        }\n        \n        int ans = 0;\n        for (int i = 0; i < n; i++)\n            if (parent[i] == i)\n                ans++;\n        \n        return ans-1;\n    }\n};\n```
[]