[C++] ✅|| Using Disjoint Set 💯|| Union by Rank 🔥
\nclass DisjointSet {\npublic:\n    vector<int> rank, parent;\n\n    DisjointSet(int n)\n    {\n        rank.resize(n + 1, 0);\n        parent.resize(n + 1);\n 
```\nclass DisjointSet {\npublic:\n    vector<int> rank, parent;\n\n    DisjointSet(int n)\n    {\n        rank.resize(n + 1, 0);\n        parent.resize(n + 1);\n        for (int i = 0; i <= n; i++) \n        {\n            parent[i] = i;\n        }\n    }\n\n    int findUPar(int node)\n    {\n        if (node == parent[node])    return node;\n        return parent[node] = findUPar(parent[node]);\n    }\n\n    void unionByRank(int u, int v)\n    {\n        int ulp_u = findUPar(u);\n        int ulp_v = findUPar(v);\n\n        if (ulp_u == ulp_v)                   return;\n        if (rank[ulp_u] < rank[ulp_v])        parent[ulp_u] = ulp_v;\n        else if (rank[ulp_v] < rank[ulp_u])   parent[ulp_v] = ulp_u;\n        else \n        {\n            parent[ulp_v] = ulp_u;\n            rank[ulp_u]++;\n        }\n    }\n};\n\nclass Solution {\npublic:\n    int findCircleNum(vector<vector<int>>& isConnected) \n    {\n        // Using disjoint set:\n        int n = isConnected.size();\n        DisjointSet ds(n);\n\n        for (int i = 0; i < isConnected.size(); i++)\n        {\n            for (int j = 0; j < isConnected[0].size(); j++) \n            {\n                if (isConnected[i][j] == 1)\n                    ds.unionByRank(i, j);\n            }\n        }\n\n        int totalComponent = 0;\n        for (int i = 0; i < n; i++)\n        {\n            if (ds.findUPar(i) == i)   totalComponent += 1;\n        }\n\n        return totalComponent;\n    }\n};\n```
['C++']