classSolution:deffindRedundantConnection(self,edges: List[List[int]]) -> List[int]: n =len(edges)for i inrange(n-1, -1, -1): leftEdges = edges[:i]+ edges[i+1:] uf =UnionFind(n)for a, b in leftEdges: uf.union(a, b)if uf.count ==1:return edges[i]
# 并查集实现代码同上,略classSolution:deffindRedundantConnection(self,edges: List[List[int]]) -> List[int]: n =len(edges) uf =UnionFind(n)for a, b in edges:if uf.isConnected(a, b):return [a, b]else: uf.union(a,b)