<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>

from collections import defaultdict
class DSU:
    def __init__(self):
        self.parents = {} 
        self.compoents_size = defaultdict(lambda: 1) # size of that compoent deafult to 1
        self.max_componet = 0


    def find(self, x):
        parent = self.parents.get(x,x)
        if parent != x:
            self.parents[x] = self.find(parent)
        return self.parents.get(x,x)

    def union(self, x, y):
        x_parent, y_parent = self.find(x), self.find(y)
        if x_parent != y_parent: # try to find 
            self.parents[x_parent] = y_parent
            self.compoents_size[y_parent] += self.compoents_size[x_parent]
            self.max_componet = max(self.max_componet, self.compoents_size[y_parent])
            # self.compoents_size[x_parent] += self.compoents_size[y_parent] # might not be necessary
            # print(x, y, self.compoents_size[y_parent])
            return True
        else: 
            return False

class Solution:
    def largestIsland(self, grid: List[List[int]]) -> int:
        N = len(grid)
        dirs = [(0, 1), (0, -1), (-1, 0), (1, 0)]
        dsu = DSU()

        for i in range(N):
            for j in range(N):
                if grid[i][j] == 1: # try to connet with neighbors
                    for oi, oj in dirs:
                        x, y = i+oi, j+oj
                        # if it's in the range and 1
                        if x in range(N) and y in range(N) and grid[x][y]==1:
                            dsu.union((i, j), (x, y))

        # print(dsu.parents)
        # print(dsu.compoents_size)

        # find 0 and find the coponts 
        max_island = max(1, dsu.max_componet)
        for i in range(N):
            for j in range(N):
                if grid[i][j] == 0: # try to connet with neighbors
                    neibors = {}
                    for oi, oj in dirs:
                        x, y = i+oi, j+oj
                        # if it's in the range and 1
                        if x in range(N) and y in range(N) and grid[x][y]==1:
                            parent = dsu.find((x, y)) 
                            value = dsu.compoents_size[parent]
                            neibors[parent] = value # suijg hash will avoid apreionts
                        
                    total_values = 1 # flipped one
                    for value in neibors.values():
                        total_values += value
                    max_island = max(max_island, total_values)

        return max_island
    
        
49618