Union Find || Please upvote if you like it :)
\nclass DSU {\npublic:\n\tvector<int> parent, size;\n\npublic:\n\tDSU(int n) {\n\t\tfor (int i = 0; i <= n; i++) {\n\t\t\tparent.push_back(i);\n\t\t\tsize.push_
```\nclass DSU {\npublic:\n\tvector<int> parent, size;\n\npublic:\n\tDSU(int n) {\n\t\tfor (int i = 0; i <= n; i++) {\n\t\t\tparent.push_back(i);\n\t\t\tsize.push_back(1);\t\t\n\t\t}\n\t}\n\n\npublic:\n\tint findPar(int node) {\n\t\tif (parent[node] == node) {\n\t\t\treturn node;\n\t\t}\n\t\treturn parent[node] = findPar(parent[node]);\n\t}\n\npublic:\n\tvoid unionSize(int u, int v) {\n\t\tint pu = findPar(u);\n\t\tint pv = findPar(v);\n\t\tif (pu == pv) {\n\t\t\treturn;\n\t\t}\n\t\tif (size[pu] < size[pv]) {\n\t\t\tparent[pu] = pv;\n\t\t\tsize[pv] += size[pu];\n\t\t}\n\t\telse {\n\t\t\tparent[pv] = pu;\n\t\t\tsize[pu] += size[pv];\n\t\t}\n\t}\n};\n\nclass Solution {\npublic:\n    int makeConnected(int n, vector<vector<int>>& connections) {\n        int m = connections.size();\n        DSU dsu(n); \n        int cnt = 0 ;\n        for(auto it : connections){\n            int a = dsu.findPar(it[0]);\n            int b = dsu.findPar(it[1]);\n            dsu.unionSize(it[0], it[1]);\n            \n        }\n        if((n - m) > 1 )return -1;\n       for(int i=0;i<n;i++){\n           if(dsu.parent[i] == i)cnt++;   \n           }\n        return cnt-1; \n    }\n};\n```
['Union Find', 'C']