leetcode-685-冗余连接②
Posted 真不知道叫啥好
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode-685-冗余连接②相关的知识,希望对你有一定的参考价值。
题目描述:
参考后提交:并查集:
class Solution: def findRedundantDirectedConnection(self, edges: List[List[int]]) -> List[int]: def find(f,x): f.setdefault(x,x) if f[x] != x: f[x] = find(f,f[x]) return f[x] def cycle(graph): f = {} for x,y in graph: if find(f,x) == find(f,y): return True else: f[find(f,y)] = find(f,x) indegree = {i:0 for i in range(1,len(edges)+1)} tmp = 0 for i,j in edges: indegree[j] += 1 if indegree[j] == 2: tmp = j break if tmp == 0: f = {} for x,y in edges: if find(f,x) == find(f,y): return [x,y] else: f[find(f,y)] = find(f,x) else: for x,y in edges[::-1]: if y == tmp: if not cycle(edges[:edges.index([x,y])]+edges[edges.index([x,y])+1:]) : return [x,y]
以上是关于leetcode-685-冗余连接②的主要内容,如果未能解决你的问题,请参考以下文章