<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 UnionFind:
    def __init__(self):
        self.parent = {}
        self.size = {}
    
    def add(self, x):
        if x not in self.parent:
            self.parent[x] = None
            self.size[x] = 1

    def find(self, x):
        root = x
        while self.parent[root] != None:
            root = self.parent[root]
        
        while x != root:
            self.parent[x], x = root, self.parent[x]
        
        return root
    
    def union(self, x, y):
        rx, ry = self.find(x), self.find(y)
        if rx == ry:
            return

        if self.size[rx] > self.size[ry]:
            rx, ry = ry, rx

        self.size[ry] += self.size[rx]
        self.parent[rx] = ry

class Solution:
    def largestIsland(self, grid: List[List[int]]) -> int:
        m, n = len(grid), len(grid[0])
        uf = UnionFind()

        for i in range(m):
            for j in range(n):
                if grid[i][j]:
                    uf.add(i * n + j)
                    for r, c in ((i - 1, j), (i, j - 1)):
                        if 0 <= r < m and 0 <= c < n and grid[r][c]:
                            uf.union(i * n + j, r * n + c)
        
        ans = max(uf.size.values(), default = 0)
        # 枚举每一个0，将其翻转
        for i in range(m):
            for j in range(n):
                if not grid[i][j]:
                    total = 1
                    used = set()   # 防止重复计算拥有同一集合的邻居比如[[1, 1], [1, 0]]中的0的邻居是同一个集合内的
                    for r, c in ((i - 1, j), (i, j - 1), (i + 1, j), (i, j + 1)):
                        if not (0 <= r < m and 0 <= c < n):
                            continue
                        if grid[r][c] == 0:
                            continue
                        if uf.find(r * n + c) in used:
                            continue
                        cur_root = uf.find(r * n + c)
                        total += uf.size[cur_root]
                        used.add(cur_root)
                    ans = max(total, ans)
        
        return ans
40356