[C++] Disjoint Set Union with Path Compression Optimization
\n\nclass Solution {\npublic:\n    int parent[201];\n    \n    int findCircleNum(vector<vector<int>>& M) {\n        int i, j, groups = 0, n = M.size();\n       
\n```\nclass Solution {\npublic:\n    int parent[201];\n    \n    int findCircleNum(vector<vector<int>>& M) {\n        int i, j, groups = 0, n = M.size();\n        make_set(n);\n          \n        for(i = 0; i < n; i++) {\n            for(j = i + 1; j < n; j++) {\n                if(M[i][j])\n                    union_sets(i, j);\n            }\n        }\n        \n        for(i = 0; i < n; i++) {\n            if(i == parent[i])\n                groups++;\n        }\n        \n        return groups;\n    }\n\nprivate:\n    void make_set(int n) {\n        for(int i = 0; i < n; i++) \n            parent[i] = i;\n    }\n    \n    int find_set(int v) {\n        if (v == parent[v])\n            return v;\n        return parent[v] = find_set(parent[v]);\n    }\n    \n    void union_sets(int a, int b) {\n        a = find_set(a);\n        b = find_set(b);\n        if (a != b)\n            parent[b] = a;\n    }\n};\n```\n\nRefer to [this](https://cp-algorithms.com/data_structures/disjoint_set_union.html) article for more details on DSU.
['Union Find', 'C']