Solution
C++ []\nclass Solution {\npublic:\n    class DisjointSet {\n    public: \n        vector<int> rank, parent, size;\n        DisjointSet(int n) {\n            ran
```C++ []\nclass Solution {\npublic:\n    class DisjointSet {\n    public: \n        vector<int> rank, parent, size;\n        DisjointSet(int 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        int findparent(int node) {\n            if(node == parent[node])\n                return node; \n            return parent[node] = findparent(parent[node]); \n        }\n        void unionByRank(int x, int y) {\n            int u = findparent(x); \n            int v = findparent(y); \n            if(u == v) return; \n            if(rank[u] < rank[v]) {\n                parent[u] = v; \n            }\n            else if(rank[v] < rank[u]) {\n                parent[v] = u; \n            }\n            else {\n                parent[v] = u; \n                rank[u]++; \n            }\n        }\n        void unionBySize(int x, int y) {\n            int u = findparent(x); \n            int v = findparent(y); \n            if(u == v) return; \n            if(size[u] < size[v]) {\n                parent[u] = v; \n                size[v] += size[u]; \n                size[u]=1;\n            }\n            else {\n                parent[v] = u;\n                size[u] += size[v];\n            }\n        }\n    }; \n    int regionsBySlashes(vector<string>& grid) {\n      int m=grid.size();\n      int n=grid[0].size();;\n      \n      int maxrow=m+1;\n      int maxcol=n+1;\n\n      DisjointSet ds(maxrow*maxcol);\n\n      int ans=1;\n\n      for(int i=0;i<maxrow;i++){\n          for(int j=0;j<maxcol;j++){\n              if( i==0 || j==0 || i==maxrow-1 || j==maxcol-1 ){\n                  int rownode=i*maxrow+j;\n                  if(rownode!=0)ds.unionBySize(0,rownode);\n              }\n          }\n      }  \n      for(int i=0;i<m;i++){\n          for(int j=0;j<n;j++){\n              if(grid[i][j]==\'/\'){\n                  int x1=i;\n                  int y1=j+1;\n                  int x2=i+1;\n                  int y2=j;\n                  int node1=x1*maxrow+y1;\n                  int node2=x2*maxrow+y2;\n                  if(ds.findparent(node1)!=ds.findparent(node2)){\n                      ds.unionBySize(node1,node2);\n                  }else{\n                      ans++;\n                  }\n              }else if(grid[i][j]==\'\\\\\'){\n                  int x1=i;\n                  int y1=j;\n                  int x2=i+1;\n                  int y2=j+1;\n                  int node1=x1*maxrow+y1;\n                  int node2=x2*maxrow+y2;\n                  if(ds.findparent(node1)!=ds.findparent(node2)){\n                      ds.unionBySize(node1,node2);\n                  }else{\n                      ans++;\n                  }\n              }\n          }\n      } \n      return ans;\n    }\n};\n```\n\n```Python3 []\nclass DSU:\n    def __init__(self, n):\n        self.parent = [i for i in range(n*n)]\n        \n        for i in range(n):\n            self.parent[i] = 0\n            self.parent[(n-1)*n + i] = 0\n        \n        for i in range(n):\n            self.parent[i*n] = 0\n            self.parent[(n-1) + i*n] = 0\n\n    def find(self, x):\n        if self.parent[x] != x:\n            return self.find(self.parent[x])\n            \n        else:\n            return x\n    \n    def union(self, x, y):\n        px, py = self.find(x), self.find(y)\n        self.parent[py] = px\n        \n        return px == py\n\nclass Solution:\n    def regionsBySlashes(self, grid: List[str]) -> int:\n        \n        ans = 0\n        n = len(grid) + 1\n        dsu = DSU(n)\n        \n        for i, row in enumerate(grid):\n            for j, x in enumerate(row):\n                if x == "/":\n                    ans += dsu.union(i*n + j + 1, (i+1)*n + j)\n                \n                elif x == "\\\\":\n                    ans += dsu.union(i*n + j, (i+1)*n + j + 1)\n        \n        return ans + 1\n```\n\n```Java []\nclass Solution {\n    static int [] rank;\n    static int [] p ;\n    public int regionsBySlashes(String[] grid) {\n        int n = grid.length;\n        int m = n+1;\n        rank = new int[m*m];    \n        p = new int[m*m];\n        int ans = 1;\n        \n        for(int i=0;i<m*m;i++)\n        {\n            p[i]=i;\n            rank[i]=1;\n        }\n        for(int i=0;i<m;i++)\n        {\n            union(0,i);\n            union(0,m*m-1-i);\n            union(0,m*i);\n            union(0,m-1 + m*i);\n        }\n       for(int i=0;i<n;i++){\n        \n        char[] arr = grid[i].toCharArray();\n        \n        for(int j=0;j<n;j++){\n           if(arr[j]==\'/\'){\n             int p1 = m*(i+1) + j;\n             int p2 = m*(i) + j+1;\n             if(union(p1,p2)) ans++;\n           }else if(arr[j]==\'\\\\\'){\n             int p1 = m*(i) + j;\n             int p2 = m*(i+1) + j+1;\n             if(union(p1,p2)) ans++;\n           }\n         }\n       }\n        return ans;\n    }\n    static int find(int x)\n    {\n        if(p[x]==x) return x;\n        int temp = find(p[x]);\n        p[x] = temp;\n        return temp;\n    }\n    static boolean union(int x,int y)\n    {\n        int px = find(x);\n        int py = find(y);\n        \n        if(px!=py)\n        {\n           if(rank[px]>rank[py]) p[py] = px;   \n           else if(rank[px]<rank[py])   p[px] = py;  \n            else{\n                p[px] = py;\n                rank[py]++;\n            }\n            return false;\n        }\n        return true;\n    }\n}\n```\n
['C++', 'Java', 'Python3']