<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 Solution {
public:
    static const int MX = 250005;
    int parent[MX];
    int rank[MX];
    int xa[4] = {0, 1, -1, 0};
    int ya[4] = {1, 0, 0 , -1};
    int ans = 1;

    int get_idx(int i, int j, int n) {
        return i * n + j;
    }

    void do_union(int s1, int s2) {
        // cout << "union " << s1 << " " << s2 << endl;
        int p1 = do_find(s1);
        int p2 = do_find(s2);

        if(p1 == p2) return;

        if(rank[p1] < rank[p2]) {
            parent[p1] = p2;
            rank[p2] += rank[p1];
        } else {
            parent[p2] = p1;
            rank[p1] += rank[p2];
        }
    }

    int do_find(int curr) {
        // cout << curr << " " << parent[curr] << endl;
        if(parent[curr] == -1) {
            return curr;
        }
        return parent[curr] = do_find(parent[curr]);
    }

    void dfs(int x, int y, vector<vector<int>>& grid, vector<vector<int>>& vis) {

        if(vis[x][y]) return;
        int n = grid.size();
        vis[x][y] = 1;

        for(int i = 0; i < 4; i++) {
            int x1 = x + xa[i], y1 = y + ya[i];
            if(x1 < 0 || x1 >= n || y1 < 0 || y1 >= n || grid[x1][y1] == 0) continue;
            do_union(get_idx(x, y, n), get_idx(x1, y1, n));
            dfs(x1, y1, grid, vis);
        }

    }

    int largestIsland(vector<vector<int>>& grid) {

        int n = grid.size();
        vector<vector<int>> vis(n, vector<int>(n));

        for(int i = 0; i < MX; i++) {
            parent[i] = -1;
            rank[i] = 1;
        }

        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                // cout << grid[i][j] << " ";
                if(grid[i][j] == 0 || vis[i][j] == 1) continue;
                int idx = get_idx(i, j, n);
                dfs(i, j, grid, vis);
            }
            // cout << endl;
        }

        // for(int p = 0; p < 4; p++) cout << "parent " << p << " -> " << parent[p] << ", " << rank[p] << endl;

        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                if(grid[i][j] == 1) {
                    ans = max(ans, rank[do_find(get_idx(i, n, n))]);
                    continue;
                }

                set<int> adj;
                int total = 0;
                for(int k = 0; k < 4; k++) {
                    int x = i + xa[k], y = j + ya[k];
                    if(x < 0 || x >= n || y < 0 || y >= n || grid[x][y] == 0) continue;
                    int p = do_find(get_idx(x, y , n));

                    if(adj.find(p) != adj.end()) continue;
                    adj.insert(p);
                    total += rank[p];
                }

                ans = max(ans, 1 + total);
            }
        }

        return ans;
    }
};
107184