-
Notifications
You must be signed in to change notification settings - Fork 2
/
redundant_connection.py
31 lines (29 loc) · 1.07 KB
/
redundant_connection.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
from collections import deque
class UnionFind:
def __init__ (self, n):
self.parents = [i for i in range(n + 1)]
self.sizes = [1 for i in range(n + 1)]
self.cap = n
def find (self, node):
stk = deque()
while (self.parents[node] != node):
stk.append(node)
node = self.parents[node]
while (stk):
self.parents[stk.pop()] = node
return node
def union (self, node1, node2):
parent1, parent2 = self.find(node1), self.find(node2)
if (parent1 != parent2):
if (self.sizes[parent2] > self.sizes[parent1]):
parent1, parent2 = parent2, parent1
self.parents[parent2] = parent1
self.sizes[parent1] += self.sizes[parent2]
return True
return False
class Solution:
def findRedundantConnection (self, edges: List[List[int]]) -> List[int]:
ans, uf = [None, None], UnionFind(len(edges))
for start, end in edges:
if (not uf.union(start, end)): ans[0], ans[1] = start, end
return ans