1192. Critical Connections in a Network (H)

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

A critical connection is a connection that, if removed, will make some servers 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.

Example 2:

Input: n = 2, connections = [[0,1]]
Output: [[0,1]]

Constraints:

  • 2 <= n <= 105

  • n - 1 <= connections.length <= 105

  • 0 <= ai, bi <= n - 1

  • ai != bi

  • There are no repeated connections.

Solution:

https://www.jiuzhang.com/problem/critical-connections-in-a-network/ DFS,graph中环的每条边都不是critical connections

通过看Youtube频道"小小福LeetCode" https://www.youtube.com/watch?v=mKUsbABiwBI的解释后写的Java代码

整体的思路就是, 把带环的删掉, 剩下的就是关键链接。 具体方法就是, 用dfs的层级来代表啥时候visit过, 如果发现之前就visit过, 那么说明有环, 把整个环删掉。 并且整个环都要知道下最小的那个depth在哪里

Last updated

Was this helpful?