-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.py
37 lines (33 loc) · 1004 Bytes
/
Solution.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
36
37
class UnionFind:
def __init__(self, n):
self.p = list(range(n))
self.n = n
def union(self, a, b):
if self.find(a) == self.find(b):
return False
self.p[self.find(a)] = self.find(b)
self.n -= 1
return True
def find(self, x):
if self.p[x] != x:
self.p[x] = self.find(self.p[x])
return self.p[x]
class Solution:
def findRedundantDirectedConnection(self, edges: List[List[int]]) -> List[int]:
n = len(edges)
p = list(range(n + 1))
uf = UnionFind(n + 1)
conflict = cycle = None
for i, (u, v) in enumerate(edges):
if p[v] != v:
conflict = i
else:
p[v] = u
if not uf.union(u, v):
cycle = i
if conflict is None:
return edges[cycle]
v = edges[conflict][1]
if cycle is not None:
return [p[v], v]
return edges[conflict]