C++ 100% Faster | Visualization Diagram | Easy to Understand | DSU (Well Commented)
\nclass Solution {\npublic:\n     \n   \n   vector< int > par;\n\tint findPar(int u)\n    {\n        if(par[u]==u)\n        {\n            return u;\n        }\
```\nclass Solution {\npublic:\n     \n   \n   vector< int > par;\n\tint findPar(int u)\n    {\n        if(par[u]==u)\n        {\n            return u;\n        }\n        return findPar(par[u]);\n    }\n    \n    int regionsBySlashes(vector<string>& grid) {\n        \n        int n = grid.size();\n        int N = n+1;\n        \n        /*Approach: For a grid of nxn, make a N+1xN+1 Matrix, which is divided into \n        pixels like, for a 2x2, make this --->\n        \n        (0,0)-----(0,1)-----(0,2)\n        |          |          |\n        |          |          |\n        (1,0)-----(1,1)-----(1,2)\n        |          |          |\n        |          |          |\n        (2,0)-----(2,1)----(2,2)\n        \n        For the starting, Consider that the borders ={0,0},{0,1},{0,2},{1,0},{1,1},{1,2}\n        {2,0},{2,1},{2,2} have a Parent at (0,0) to represent a figured of graph\n        \n        \n        Npw, there will be two cases:\n        \n        Case 1 --->\n        At grid[row][col], I have \'/\' ---->\n        \n        here,   (r,c)---(r,c+1)\n                    |  /\n                    | /\n                    |/       \n                 (r+1, c)  so, for (r,c) => Union find (r+1,c) & (r, c+1)\n        \n        Case 2 ---->\n        At grid[row][col], I have \'\\\\\'  --->\n        \n        here,   (r,c)\n                    \\\n                     \\\n                      \\\n                       (r+1, c+1) ====> So, Union find (r, c) & (r+1, c+1)\n                       \n        Last Step : Calculation of answers,\n        \n        for this, whenever we detect a cycle during Union Find, increment the answer as \n        the borders are already connected to a single parent meaning there will be a divide*/\n        \n        \n        \n        par.resize(N*N);\n        \n        for(int i=0;i<N;i++)\n        {\n            for(int j=0;j<N;j++)\n            {\n                if(i==0 || j==0 || i==N-1 || j==N-1)\n                {\n                    par[i*N+j]=0; //All Borders & edges are parent at 0\n                }else{\n                par[i*N+j]=i*N+j;\n                }\n            }\n        }\n      \n        int cycle = 1;\n        \n        for(int i=0;i<n;i++)\n        {\n            for(int j=0;j<n;j++)\n            {\n                if(grid[i][j]==\'/\')\n                {\n                    int cord1 = i*N + (j+1);\n                    int cord2 = (i+1)*N + j;\n                    int p1 = findPar(cord1);\n                    int p2 = findPar(cord2);\n                     \n                    if(p1==p2)\n                    {\n                         \n                        cycle++;\n                    }else if(p1!=p2){\n                        par[p1]=p2;\n                    }\n                }else if(grid[i][j]==\'\\\\\')\n                {\n                    int cord1 = i*N + (j);\n                    int cord2 = (i+1)*N + (j+1);\n                    int p1 = findPar(cord1);\n                    int p2 = findPar(cord2);\n                    \n                    if(p1==p2)\n                    {\n                        cycle++;\n                    }else if(p1!=p2){\n                        par[p1]=p2;\n                    }\n                }\n            }\n        }\n        return cycle;\n        \n    }\n};\n```
['Union Find', 'Graph', 'C', 'C++']