Talentd
FleetCode

1192. Critical Connections in a Network

Hard60.6% acceptance8 companies

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.

Hints open one at a time

Hint 1
Use Tarjan's algorithm.
29:14
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def criticalConnections(self, n: int, connections: List[List[int]]) -> List[List[int]]:
3
Starter codePython 3Spaces: 4
Examples
Input
4
[[0,1],[1,2],[2,0],[1,3]]
Input
2
[[0,1]]
Your code is saved on this device.Solution