<p>You are given an <code>n x n</code> binary matrix <code>grid</code>. You are allowed to change <strong>at most one</strong> <code>0</code> to be <code>1</code>.</p>

<p>Return <em>the size of the largest <strong>island</strong> in</em> <code>grid</code> <em>after applying this operation</em>.</p>

<p>An <strong>island</strong> is a 4-directionally connected group of <code>1</code>s.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,0],[0,1]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,1],[1,0]]
<strong>Output:</strong> 4
<strong>Explanation: </strong>Change the 0 to 1 and make the island bigger, only one island with area = 4.</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,1],[1,1]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Can&#39;t change any 0 to 1, only one island with area = 4.
</pre>

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

<ul>
	<li><code>n == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= n &lt;= 500</code></li>
	<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>

class DSU{
    public:
    vector<vector<pair<int,int>>>parent;
    vector<vector<int>>rank;
    vector<vector<int>>size;
    int n,m;
    DSU(int n,int m){
        this->n=n;
        this->m=m;
        parent.resize(n,vector<pair<int,int>>(m,{-1,-1}));
        rank.resize(n,vector<int>(m,0));
        size.resize(n,vector<int>(m,0));
    }

    pair<int,int>find(int a,int b){
        if(parent[a][b].first==a && parent[a][b].second==b) return {a,b};
        return parent[a][b]=find(parent[a][b].first,parent[a][b].second);
    }

    void add(int a,int b){
        parent[a][b]={a,b};
        size[a][b]=1;
        vector<int>dx={0,1,0,-1};
        vector<int>dy={1,0,-1,0};
        for(int i=0;i<4;i++){
            int na=a+dx[i];
            int nb=b+dy[i];
            if(na>=0 && na<n && nb>=0 && nb<m && parent[na][nb].first!=-1){
                make_union(a,b,na,nb);
            }
        }
    }

    void make_union(int a,int b,int c,int d){
        auto it1=find(a,b);
        auto it2=find(c,d);
        int pa=it1.first;
        int pb=it1.second;
        int pc=it2.first;
        int pd=it2.second;

        if(pa==pc && pb==pd) return;

        if(rank[pa][pb]>rank[pc][pd]){
            parent[pc][pd]={pa,pb};
            size[pa][pb]+=size[pc][pd];
        }
        else if(rank[pa][pb]<rank[pc][pd]){
            parent[pa][pb]={pc,pd};
            size[pc][pd]+=size[pa][pb];
        }
        else{
            parent[pa][pb]={pc,pd};
            size[pc][pd]+=size[pa][pb];
            rank[pc][pd]++;
        }

    }

    int getMaxSize(){
        int ans=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                ans=max(ans,size[i][j]);
            }
        }
        return ans;
    }

    int flip(int a,int b){
        set<pair<int,int>>st;
        int ans=1;
        vector<int>dx={0,1,0,-1};
        vector<int>dy={1,0,-1,0};
        for(int i=0;i<4;i++){
            int na=a+dx[i];
            int nb=b+dy[i];
            if(na>=0 && nb>=0 && na<n && nb<m && parent[na][nb].first!=-1){
                auto it=find(na,nb);
                if(st.find({it.first,it.second})==st.end()){
                    st.insert({it.first,it.second});
                    ans+=size[it.first][it.second];
                }
            }
        }
        return ans;
    }

};


class Solution {
public:
    int largestIsland(vector<vector<int>>& grid) {
        int n=grid.size();
        int m=grid[0].size();
    

    DSU dsu=DSU(n,m);
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(grid[i][j]==1){
                dsu.add(i,j);
            }
        }
    }
    int ans=dsu.getMaxSize();
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(grid[i][j]==0){
                ans=max(ans,dsu.flip(i,j));
            }
        }
    }
    return ans;
    }
};
241846