0
点赞
收藏
分享

微信扫一扫

1192. Critical Connections in a Network刷题笔记


参考这个题解,用的dfs

import collections
class Solution:
    def criticalConnections(self, n: int, connections: List[List[int]]) -> List[List[int]]:
        def makeGraph(coonections):
            graph = collections.defaultdict(list)
            for conn in connections:
                graph[conn[0]].append(conn[1])
                graph[conn[1]].append(conn[0])
            return graph
        
        graph = makeGraph(connections)
        connections = set(map(tuple,map(sorted,connections)))
        rank = [-2]*n
        
        def dfs(node, depth):
            if rank[node] >= 0:
                return rank[node]
            rank[node] = depth
            min_back_depth = n
            for neighbor in graph[node]:
                if rank[neighbor] == depth-1:
                    continue
                back_path = dfs(neighbor, depth+1)
                if back_path <= depth:
                    connections.discard(tuple(sorted((node,neighbor))))
                min_back_depth = min(min_back_depth,back_path)
            rank[node] = n
            return min_back_depth
        dfs(0,0)
        return list(connections)

1192. Critical Connections in a Network刷题笔记_List


举报

相关推荐

python刷题笔记

LeetCode刷题笔记

【leetcode刷题笔记】

【Leetcode】刷题笔记

刷题笔记篇

刷题笔记2

Leetcode刷题笔记

刷题笔记ing

算法刷题笔记

0 条评论