在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ There are 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 = 4, connections = [[0,1],[1,2],[2,0],[1,3]] 输出:[[1,3]] 解释:[[3,1]] 也是正确的。
提示:
Runtime: 2832 ms
Memory Usage: 52.7 MB
1 class Solution { 2 var v:[[Int]] = [[Int]]() 3 var dfn:[Int] = [Int]() 4 var low:[Int] = [Int]() 5 var times:Int = 0 6 var ret:[[Int]] = [[Int]]() 7 8 func criticalConnections(_ n: Int, _ connections: [[Int]]) -> [[Int]] { 9 v = [[Int]](repeating:[Int](),count:n) 10 dfn = [Int](repeating:0,count:n) 11 low = [Int](repeating:0,count:n) 12 for e in connections 13 { 14 v[e[0]].append(e[1]) 15 v[e[1]].append(e[0]) 16 } 17 for i in 0..<n 18 { 19 if dfn[i] == 0 20 { 21 tarjan(i, -1) 22 } 23 } 24 return ret 25 } 26 27 func tarjan(_ x:Int,_ p:Int) 28 { 29 times += 1 30 dfn[x] = times 31 low[x] = times 32 for y in v[x] 33 { 34 if y == p {continue} 35 if dfn[y] == 0 36 { 37 tarjan(y, x) 38 low[x] = min(low[x], low[y]) 39 if low[y] > dfn[x] 40 { 41 ret.append([x, y]) 42 } 43 } 44 else 45 { 46 low[x] = min(low[x], dfn[y]) 47 } 48 } 49 } 50 }
|
请发表评论