@@ -22,7 +22,7 @@ def point_to_idx(x, y):
2222 for dx , dy in DIRS :
2323 if 0 <= (nx := i + dx ) < n and 0 <= (ny := j + dy ) < n and grid [nx ][ny ] == 1 :
2424 uf .union (p , point_to_idx (nx , ny ))
25- ans = max (uf .count )
25+ ans = max (uf .size )
2626 for i , row in enumerate (grid ):
2727 for j , val in enumerate (row ):
2828 if val == 0 :
@@ -34,26 +34,25 @@ def point_to_idx(x, y):
3434 if root in explored :
3535 continue
3636 explored .add (root )
37- tot += uf .count [root ]
37+ tot += uf .size [root ]
3838 ans = max (ans , tot )
3939
4040 return ans
4141
4242class UnionFind :
43- def __init__ (self , size ):
44- self .parent = list (range (size ))
45- self .rank = [1 ] * size
46- self .count = [1 ] * size
47- self .size = size
48- self .cc = size
43+ def __init__ (self , n : int ):
44+ self .parent = list (range (n ))
45+ self .rank = [1 ] * n
46+ self .size = [1 ] * n
47+ self .cc = n
4948
50- def find (self , x ) :
49+ def find (self , x : int ) -> int :
5150 while self .parent [x ] != x :
5251 self .parent [x ] = self .parent [self .parent [x ]] # 路径压缩
5352 x = self .parent [x ]
5453 return x
5554
56- def union (self , x , y ) :
55+ def union (self , x : int , y : int ) -> bool :
5756 root_x = self .find (x )
5857 root_y = self .find (y )
5958
@@ -63,14 +62,14 @@ def union(self, x, y):
6362 # 按秩合并
6463 if self .rank [root_x ] > self .rank [root_y ]:
6564 self .parent [root_y ] = root_x
66- self .count [root_x ] += self .count [root_y ]
65+ self .size [root_x ] += self .size [root_y ]
6766 else :
6867 self .parent [root_x ] = root_y
6968 if self .rank [root_x ] == self .rank [root_y ]:
7069 self .rank [root_y ] += 1
71- self .count [root_y ] += self .count [root_x ]
70+ self .size [root_y ] += self .size [root_x ]
7271 self .cc -= 1
7372 return True
7473
75- def is_connected (self , x , y ) :
74+ def is_connected (self , x : int , y : int ) -> bool :
7675 return self .find (x ) == self .find (y )
0 commit comments