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.
A 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]
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