[C++] 90% Faster | Union Find | Comments for Easy Understanding 😲
\nclass Solution {\npublic:\n    \n    //Find Parent function\n \tint find(int x, vector<int>& parent) {\n \t\tif (parent[x] == x) return parent[x];\n        re
```\nclass Solution {\npublic:\n    \n    //Find Parent function\n \tint find(int x, vector<int>& parent) {\n \t\tif (parent[x] == x) return parent[x];\n        return parent[x] = find(parent[x], parent);\n \t}\n    \n    //Main Problem Solver Function\n    int makeConnected(int n, vector<vector<int>>& connections) {\n        \n        //Initializing parent array with their own parent values\n        vector<int> parent(n);\n        for(int i=0; i<n; i++){\n            parent[i] = i;\n        }\n        \n        //For counting components\n \t\tint compo = 0;\n        \n        for (auto& it: connections) {\n \t\t\tint x = find(it[0], parent);\n \t\t\tint y = find(it[1], parent);\n \t\t\t\n            //If parent are same that means they were connected and now again they are being connected so continue/skip\n            if (x == y){\n                continue;\n            }\n            \n            //If Parent not same, then Union them and increment compo.\n            else {\n                parent[y] = x; //union\n                compo++;\n            }\n \t\t}\n        \n        //We know to connect n computers we need n-1 cables so if connections.size() < n-1 return -1\n\t\t//else required - already connected and i.e. (n-1) - (components)\n        return connections.size() < n-1 ? -1 : n-1-compo;\n    }\n    \n};\n```
['Union Find', 'C++']