1192. Critical Connections in a Network (H)
Last updated
Last updated
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:
Example 2:
Constraints:
2 <= n <= 105
n - 1 <= connections.length <= 105
0 <= ai, bi <= n - 1
ai != bi
There are no repeated connections.
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在哪里