<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 DisjointSet {
public:
    vector<int> size, parent;
    DisjointSet(int n) {
        size.resize(n + 1, 1);
        parent.resize(n + 1);
        for (int i = 0; i <= n; i++) {
            parent[i] = i;
        }
    }
    int find(int& x) {
        if (parent[x] == x)
            return x;
        return parent[x] = find(parent[x]);
    }
    void unionBySize(int& x, int& y) {
        int ulp_x = find(x);
        int ulp_y = find(y);
        if (ulp_x == ulp_y)
            return;
        if (size[ulp_x] < size[ulp_y]) {
            size[ulp_y] += size[ulp_x];
            parent[ulp_x] = ulp_y;
        } else {
            size[ulp_x] += size[ulp_y];
            parent[ulp_y] = ulp_x;
        }
    }
};

class Solution {
private:
    const vector<vector<int>> dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
    bool isValid(int& u, int& v, int& m, int& n) {
        return (u >= 0 && v >= 0 && u < m && v < n);
    }

public:
    int largestIsland(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size();
        DisjointSet ds(m * n);
        if(m==1 && n==1)
            return 1;
        vector<vector<int>> vis(m, vector<int>(n, 0));
        int ans = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 0)
                    continue;
                int nodeNo = i * n + j;
                for (auto& d : dir) {
                    int u = i + d[0];
                    int v = j + d[1];
                    int adjNodeNo = u * n + v;
                    if (isValid(u, v, m, n) && grid[u][v] == 1) {
                        if (ds.find(nodeNo) != ds.find(adjNodeNo)) {
                            ds.unionBySize(nodeNo, adjNodeNo);
                            ans = max(ds.size[ds.find(nodeNo)], ans);
                        }
                    }
                }
            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 0) {
                    set<int> s;
                    for (auto& d : dir) {
                        int u = i + d[0];
                        int v = j + d[1];
                        int adjNodeNo = u * n + v;
                        if (isValid(u, v, m, n) && grid[u][v] == 1){
                            s.insert(ds.find(adjNodeNo));
                        }
                    }
                    int cnt=0;
                    for(int ele: s){
                        cnt+=ds.size[ds.find(ele)];
                    }
                    cnt+=1;
                    ans=max(ans, cnt);
                }
            }
        }
        return ans;
    }
};
117815