DSU Based Full Explanation | 4 ms Solution | C++
Intution : So Basically we need to think slash (\'/\' or \'\\\') represents an edge, but we don\'t have points connected with this edge. So, we must think some 
**Intution** : So Basically we need to think slash (\'/\' or \'\\\\\') represents an edge, but we don\'t have points connected with this edge. So, we must think some way to bring in vertex in this question. \n\n**Approach** : Given is a grid with size n, therefore create a matrix of size (n+1) * (n + 1) or we can say that there will be (n+1)*(n+1) vertices.\n\nFor example : n = 2\nso matrix will be of size 3*3 : \n```\n(0)--------(1)--------(2)\n  |         |          |\n  |         |          |\n(3)--------(4)-------(5)\n  |          |         |\n  |          |         |\n(6)---------(7)-------(8)\n\n```\nThis is our matrix. We need to find the no of regions created after putting slash (\'/\' or \'\\\\\') between these set of points.\n\n\nFor this, we will join all border points and make (0) as their root, and then while searching we will see if connecting two points will divide the region or not.\n\nHow we will check ?\nAns -  If the 2 points that we are connecting has same representative (leader). Suppose (1) and (3) are connected then both points have 0 as their representative already, then joining these two vertex will divide the region and our count of regions will be incremented.\n\nHow we are calculating vertex ?\nAns - Since it is square matrix, we can use this formula => ```i*N + j``` \nSample testcase : n = 2 => N = (n + 1)\n=> i =0 -> Points will be    0---------1---------2\n=> i = 1 -> Points will be   3---------4---------5\n=> i = 2 -> Points will be   6---------7---------8\n\nFinally Code:\n```\nclass Solution {\npublic:\n    vector<int> par,size;\n    int count;\n    int find_set(int u) {\n\tif (u == par[u]) return u;\n\n\treturn par[u] = find_set(par[u]);\n}\n    void make_set(int u, int v) {\n\tu = find_set(u);\n\tv = find_set(v);\n    \n    if(u == v) count++;\n\telse {\n\t\tif (size[u] < size[v])\n\t\t\tswap(u, v);\n\t\tpar[v] = u;\n\t\tsize[u] += size[v];\n\t}\n}\n    int regionsBySlashes(vector<string>& grid) {\n        int n = grid.size();\n        int N = n+1;\n        \n        par.resize(N*N);\n        size.resize(N*N);\n        for(int i = 0;i<N*N;i++){\n            par[i] = i;\n            size[i] = 1;\n        }\n        \n        \n        for(int i = 0;i< N;i++){\n            for(int j = 0;j< N;j++){\n                int point = i*N + j;\n                if(i == 0 && j == 0) continue;\n                \n                else if(i == 0 || j == 0 || i == N - 1 || j == N-1){\n                    make_set(0,point);\n                }\n            }\n        }\n        \n        count = 1;\n        \n        for(int i = 0;i<grid.size();i++){\n            for(int j = 0;j<grid[0].size();j++){\n                if(grid[i][j] == \'/\'){\n                    int p1 = i*N + j + 1;\n                    int p2 = (i+1)*N + j;\n                    \n                    make_set(p1,p2);\n                }\n                else if(grid[i][j] == \'\\\\\'){\n                    int p1 = i*N + j;\n                    int p2 = (i+1)*N + j + 1;\n                    \n                    make_set(p1,p2);\n                }\n            }\n        }\n        \n        return count;\n    }\n};\n```\n
['Graph']