DSU || C++ || Runtime 0 ms Beats 100.00% || Easy Solution
Beats 100%\n\n# Code\n\nclass Solution {\npublic:\n    vector<int> par;\n    vector<int> rank;\n    int ans = 1;\n\n    int findPar(int node){\n        if(node 
## Beats 100%\n![image_94.png](https://assets.leetcode.com/users/images/aedeff9d-4a0e-457d-b88a-e19af1009f1f_1723272336.4415221.png)\n# Code\n```\nclass Solution {\npublic:\n    vector<int> par;\n    vector<int> rank;\n    int ans = 1;\n\n    int findPar(int node){\n        if(node == par[node])    return node;\n        par[node] = findPar(par[node]);\n        return par[node];\n    }\n\n    void uni(int a, int b){\n        int parA = findPar(a);\n        int parB = findPar(b);\n        \n        if(parA != parB){\n\n            if(rank[parA] > rank[parB])         par[parB] = parA;\n            else if (rank[parA] < rank[parA])   par[parA] = parB;\n            else                                par[parB] = parA,  rank[parA]++;\n        } \n        else   ans++;\n    }\n\n    int regionsBySlashes(vector<string>& grid) {\n\n        int row = grid.size() + 1;\n        int col = grid.size() + 1;\n        int n = row * col;\n\n        par.resize(n);\n        rank.resize(n, 1);\n        for(int i = 0; i < par.size(); i++)        par[i] = i;\n        \n        for(int i = 0; i < row; i++){\n            for(int j = 0; j < col; j++){\n                if(i == 0 or j == 0 or i == row - 1 or j == col - 1){\n                    int p = i * row + j;\n                    if(p != 0) uni(0, p);\n                }\n            }\n        }\n        \n        for(int i = 0; i < grid.size(); i++){\n            for(int j = 0; j < grid.size(); j++){\n                if(grid[i][j] == \'/\'){\n                    int x = i * row + (j + 1);\n                    int y = (i + 1) * row + j;\n                    uni(x, y);\n                } \n                else if (grid[i][j] == \'\\\\\'){\n                    int x = i * row + j;\n                    int y = (i + 1) * row + (j + 1);\n                    uni(x, y);\n                }\n            }\n        }\n        \n        return ans;\n    }\n};\n```
['C++']