1192. Critical Connections in a Network

65 篇文章 0 订阅
4 篇文章 0 订阅

There are n servers numbered from 0 to n-1 connected by undirected server-to-server connections forming a network where connections[i] = [a, b] represents a connection between servers a and b. Any server can reach any other server directly or indirectly through the network.

critical connection is a connection that, if removed, will make some server unable to reach some other server.

Return all critical connections in the network in any order.

 

Example 1:

Input: n = 4, connections = [[0,1],[1,2],[2,0],[1,3]]
Output: [[1,3]]
Explanation: [[3,1]] is also accepted.

 

Constraints:

  • 1 <= n <= 10^5
  • n-1 <= connections.length <= 10^5
  • connections[i][0] != connections[i][1]
  • There are no repeated connections.

Discuss

思路:
需要先了解tarjan算法求解强联通分量的原理,可参考

https://search.bilibili.com/all?keyword=Tarjan

https://blog.csdn.net/starstar1992/article/details/52944958

然后在geeksforgeeks上找到类似的题目,看下怎么根据dfn、low数组来判断是否为割点割线

 https://www.geeksforgeeks.org/bridge-in-a-graph/ 

https://www.geeksforgeeks.org/articulation-points-or-cut-vertices-in-a-graph/

标准的套题,但是没打过ACM,不了解tarjan算法,即使比赛完自己看博客也是看的一知半解(自己还是太菜了)

from collections import defaultdict
class Solution(object):
    def criticalConnections(self, n, connections):
        """
        :type n: int
        :type connections: List[List[int]]
        :rtype: List[List[int]]
        """
        adj=defaultdict(set)
        for a,b in connections:
            adj[a].add(b)
            adj[b].add(a)
        
        vis=[False]*n
        time = [0]
        dfn = [0]*n
        low = [0]*n
        parent = [-1]*n
        res=[]
        
        def tarjan(u):
            vis[u]=True
            time[0]+=1
            dfn[u]=low[u]=time[0]
            
            for v in adj[u]:
                if not vis[v]:
                    parent[v] = u
                    tarjan(v)
                    low[u] = min(low[u], low[v])
                    
                    if low[v]>dfn[u]:
                        res.append([u,v])
                elif v!=parent[u]:
                    low[u] = min(low[u], dfn[v])
            
        for root in range(n):
            if not vis[root]:
                tarjan(root)
        return res
    

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值