<p>An <code>n x n</code> grid is composed of <code>1 x 1</code> squares where each <code>1 x 1</code> square consists of a <code>&#39;/&#39;</code>, <code>&#39;\&#39;</code>, or blank space <code>&#39; &#39;</code>. These characters divide the square into contiguous regions.</p>

<p>Given the grid <code>grid</code> represented as a string array, return <em>the number of regions</em>.</p>

<p>Note that backslash characters are escaped, so a <code>&#39;\&#39;</code> is represented as <code>&#39;\\&#39;</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/15/1.png" style="width: 200px; height: 200px;" />
<pre>
<strong>Input:</strong> grid = [&quot; /&quot;,&quot;/ &quot;]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/15/2.png" style="width: 200px; height: 198px;" />
<pre>
<strong>Input:</strong> grid = [&quot; /&quot;,&quot;  &quot;]
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/15/4.png" style="width: 200px; height: 200px;" />
<pre>
<strong>Input:</strong> grid = [&quot;/\\&quot;,&quot;\\/&quot;]
<strong>Output:</strong> 5
<strong>Explanation: </strong>Recall that because \ characters are escaped, &quot;\\/&quot; refers to \/, and &quot;/\\&quot; refers to /\.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == grid.length == grid[i].length</code></li>
	<li><code>1 &lt;= n &lt;= 30</code></li>
	<li><code>grid[i][j]</code> is either <code>&#39;/&#39;</code>, <code>&#39;\&#39;</code>, or <code>&#39; &#39;</code>.</li>
</ul>

#include<bits/stdc++.h>
#define ll long long
#define vec vector<long long>
#define ld long double
#define print(v) for(auto c:v)cout<<c<<" "
#define loop(i, n) for (int i = 0; i < n; i++)
#define print_matrix(x) for(int i=0;i<x.size();i++){print(x[i]);cout<<endl;}
using namespace std;
class DSU{
    public:
    vec rank,parent,size;
    DSU(int n){
        parent.resize(n+1); 
        rank.resize(n+1); 
        size.resize(n+1);
        loop(i,n+1){
            parent[i]=i;
            rank[i]=0;
            size[i]=1;
        }
    }
    int find(int node){
        if(parent[node]==node)return node;
        return parent[node]=find(parent[node]);
    }
    void union_rank(int a,int b){
        int parent_a=find(a);
        int parent_b=find(b);
        if(parent_a==parent_b){return;}
        else if(rank[parent_a]>rank[parent_b]){
            parent[parent_b]=parent_a;
        }
        else if(rank[parent_a]<rank[parent_b]){
            parent[parent_a]=parent_b;
        }
        else{
            parent[parent_b]=parent_a;
            rank[parent_a]++;
        }
    }
    void union_size(int a,int b){
        a=find(a);
        b=find(b);
        if(a==b)return;
        if(size[b]>size[a])swap(a,b);
        if(b!=a){
            size[a]=size[a]+size[b];
            parent[b]=a;
        }
    }
};
class Solution {
public:
    bool is_valid(int x,int y,int n){
        return x>=0&&y>=0&&x<n&&y<n;
    }
    int regionsBySlashes(vector<string>& grid) {
        int n=grid.size();
        // cout<<grid[1];
        // return 1;
       DSU dsu(4*n*n+1);
       for(int i=0;i<n;i++){
        int index=0;
        for(int j=0;j<n;j++){
            int left=(i*n+j)*4;
            int top=left+1;
            int right=top+1;
            int bottom=right+1;
            if(grid[i][j]==' '){
                dsu.union_size(left,top);
                dsu.union_size(top,right);
                dsu.union_size(right,bottom);
                dsu.union_size(bottom,left);
            }
            else if(grid[i][j]=='/'){
                dsu.union_size(left,top);
                dsu.union_size(right,bottom);
            }
            else{
                // index++;
                dsu.union_size(top,right);
                dsu.union_size(bottom,left);
            }
            // index++;
            if(right+2<i*4*n+4*n)dsu.union_size(right,right+2);
            if(bottom+4*n-2<4*n*n)dsu.union_size(bottom,bottom+4*n-2);
        }
       } 
       int ans=0;
       for(int i=0;i<4*n*n;i++){
        if(dsu.find(i)==i)ans++;
       }
       return ans;
    }
};
15293