-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution2.py
31 lines (26 loc) · 876 Bytes
/
Solution2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class UnionFind:
__slots__ = "p", "size"
def __init__(self, n: int):
self.p: List[int] = list(range(n))
self.size: List[int] = [1] * n
def find(self, x: int) -> int:
if self.p[x] != x:
self.p[x] = self.find(self.p[x])
return self.p[x]
def union(self, a: int, b: int) -> bool:
pa, pb = self.find(a), self.find(b)
if pa == pb:
return False
if self.size[pa] > self.size[pb]:
self.p[pb] = pa
self.size[pa] += self.size[pb]
else:
self.p[pa] = pb
self.size[pb] += self.size[pa]
return True
class Solution:
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
uf = UnionFind(len(edges))
for a, b in edges:
if not uf.union(a - 1, b - 1):
return [a, b]