并查集
class Solution:
def findRedundantConnection(self, edges):
n = len(edges)
bcj = {i: i for i in range(1, n + 1)}
ans = None
def find(x):
if x != bcj[x]:
bcj[x] = find(bcj[x])
return bcj[x]
def union(x, y):
a, b = find(x), find(y)
if a != b:
bcj[a] = b
return True
return False
for i, j in edges:
if not union(i, j):
ans = [i, j]
return ans