C++ Union Find with Rank and Path Compression
\nclass Solution {\npublic:\n    int find(int node, vector<int> &parent){\n        if(parent[node] == node) return node;\n\t\t// Path compression\n        retur
```\nclass Solution {\npublic:\n    int find(int node, vector<int> &parent){\n        if(parent[node] == node) return node;\n\t\t// Path compression\n        return parent[node] = find(parent[node], parent);\n    }\n    \n    int makeConnected(int n, vector<vector<int>>& connections) {\n        if(connections.size() < n-1) return -1;\n        int comp = 0;\n        vector<int> parent(n);\n        vector<int> rank(n);\n        for(int i=0; i<n; i++){\n            parent[i] = i;\n            rank[i] = 0;\n        }\n        \n        for(auto i: connections){\n            int p1 = find(i[0], parent);\n            int p2 = find(i[1], parent);\n            \n            if(rank[p1] < rank[p2]){\n                parent[p1] = p2;\n            }\n            else if(rank[p2] < rank[p1]){\n                parent[p2] = p1;\n            }\n            else{\n                parent[p1] = p2;\n                rank[p2]++;\n            }\n        }\n        \n        for(int i=0; i<n; i++){\n            if(parent[i] == i) comp++;\n        }\n        \n        return comp-1;\n    }\n};\n```
['Union Find', 'C', 'C++']