Skip to the code
+50 XP

#477 Cables Whose Failure Splits the Network

hard

An infrastructure dashboard highlights single points of failure. Servers are numbered 0 to n - 1, and cables[i] = [a, b] is a two-way cable. Two servers may be joined by more than one cable.

A critical cable is one whose removal leaves some pair of servers that were connected unable to reach each other. Write criticalCables(n, cables) that returns every critical cable as [smaller, larger], with the list sorted by the first number and then the second.

Example 1

Input: n = 5, cables = [[0,1],[1,2],[2,0],[1,3],[3,4]]
Output: [[1,3],[3,4]]

The triangle 0-1-2 survives losing any one cable, but cutting 1-3 or 3-4 separates servers.

Example 2

Input: n = 3, cables = [[0,1],[1,2],[2,0]]
Output: []

Every cable is part of a loop, so there's always another way around.

Constraints

  • 1 <= n <= 5 * 10^4
  • 0 <= cables.length <= 10^5
  • 0 <= a, b < n, a !== b
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).