-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path684-RedundantConnection.py
35 lines (31 loc) · 1.01 KB
/
684-RedundantConnection.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
32
33
34
35
"""
Reference: https://www.geeksforgeeks.org/union-find/
"""
class Solution:
def __init__(self):
self.parents = []
self.violated = []
def find(self, element):
"""find the set to which the node belongs"""
if self.parents[element-1] == -1:
return element
else:
return self.find(self.parents[element-1])
def union(self, set_a, set_b):
"""Join two sets"""
self.parents[set_b - 1] = set_a
return
def findRedundantConnection(self, edges):
"""
:type edges: List[List[int]]
:rtype: List[int]
"""
self.parents = [-1] * len(edges)
for edge in edges:
find_0 = self.find(edge[0])
find_1 = self.find(edge[1])
if find_0 == find_1:
self.violated.append(edge)
else:
self.union(find_0, find_1)
return self.violated[-1]