-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy path685.py
More file actions
19 lines (17 loc) · 678 Bytes
/
685.py
File metadata and controls
19 lines (17 loc) · 678 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
def findRedundantDirectedConnection(self, edges):
def root(i):
return parent[i] == i and i or root(parent[i])
parent, a, b, c = [0] * (len(edges) + 1), None, None, None
for i, edge in enumerate(edges):
if parent[edge[1]]:
a, b, c, edges[i][0]= parent[edge[1]], edge[0], edge[1], 0
else:
parent[edge[1]] = edge[0]
parent = [i for i in range(len(edges) + 1)]
for u, v in edges:
if u:
if root(u) == v:
return a and [a, c] or [u, v]
parent[v] = u
return [b, c]