🌟🌟Full Handwritten Explanation+ Approach || DSU ||90% Fast||Kruskal's Algorithm
\n# Explanation and Approach\n Describe your approach to solving the problem. \n\n\n\n\n\n# Code\n\n\nclass DisjointSet {\npublic:\nvector<int> rank, parent, si
\n# Explanation and Approach\n<!-- Describe your approach to solving the problem. -->\n![image.png](https://assets.leetcode.com/users/images/fc316e9f-b4ee-48ab-9bc1-1137327bd184_1688149676.623193.png)\n![image.png](https://assets.leetcode.com/users/images/9321a6e0-95df-4f22-af99-3b725f3da3c0_1688149705.92701.png)\n![image.png](https://assets.leetcode.com/users/images/d1f78c56-4c85-4a0b-a1ba-1235a5a9a52a_1688149746.0478876.png)\n![image.png](https://assets.leetcode.com/users/images/6210d9d4-0a75-4a08-94a7-a5520ef41e7f_1688149799.3202717.png)\n\n# Code\n```\n\nclass DisjointSet {\npublic:\nvector<int> rank, parent, size;\n   DisjointSet(int n)\n{\n       rank.resize(n+1, 0);\n       parent.resize(n+1);\n       size.resize(n+1);\n       for(int i = 0;i<=n;i++) {\n           parent[i] = i;\n           size[i] = 1;\n       }\n   }\n \n   int findUPar(int node)\n  {\n       if(node == parent[node])\n           return node;\n       return parent[node] = findUPar(parent[node]);\n   }\n \n   void unionBySize(int u, int v)\n {\n       int ulp_u = findUPar(u);\n       int ulp_v = findUPar(v);\n       if(ulp_u == ulp_v) return;\n       if(size[ulp_u] < size[ulp_v]) {\n           parent[ulp_u] = ulp_v;\n           size[ulp_v] += size[ulp_u];\n       }\n       else {\n           parent[ulp_v] = ulp_u;\n           size[ulp_u] += size[ulp_v];\n       }\n   }\n};\nclass Solution {\npublic:\n    int regionsBySlashes(vector<string>& grid) \n    {\n        \n        int rows=grid.size();\n        int cols=grid[0].size();\n\n        int tdots=(rows+1)*(cols+1);\n        DisjointSet ds(tdots);\n\n        //Connecting the border dots\n        for(int r=0;r<=rows;r++)\n        {\n            for(int c=0;c<=cols;c++)\n            {\n                if(r==0 || r==rows || c==0 || c==cols)\n                {\n                    int dot=(r*(cols+1))+c;\n                    // print(dot);\n                    ds.unionBySize(0,dot);\n                }\n            }\n        }\n        int comp=1;\n        //Travering the grid and connecting\n        for(int r=0;r<rows;r++)\n        {\n            for(int c=0;c<cols;c++)\n            {\n                if(grid[r][c]==\'/\')\n                {\n                    int dot1=((r+1)*(cols+1))+c;\n                    int dot2=((r)*(cols+1))+c+1;\n                    if(ds.findUPar(dot1)==ds.findUPar(dot2))\n                    {\n                        // cout<<dot1<<" / "<<dot2<<endl;\n                        comp+=1;\n                    }\n                        \n                    ds.unionBySize(dot1,dot2);\n                }\n                else if(grid[r][c]==\'\\\\\')\n                {\n                    int dot1=((r)*(cols+1))+c;\n                    int dot2=((r+1)*(cols+1))+c+1;\n                    if(ds.findUPar(dot1)==ds.findUPar(dot2))\n                    {\n                        // cout<<dot1<<" \\\\ "<<dot2<<endl;\n                        comp+=1;\n                    }\n                    ds.unionBySize(dot1,dot2);\n                }\n            }\n        }\n\n        return comp;\n    }\n};\n```
['Union Find', 'C++']