Problem 536359 · hard · Phase 05 Advanced Algorithms & Graphs

The Most Damaging Cable Cut

DFS · bridges · low-link · subtree sizes · iterative DFS

A data centre has n servers 0 .. n-1 and a list cables, where cables[i] = [u, v] is an undirected cable between two different servers. Two cables may join the same pair of servers, and the network need not be connected.

If cable i fails, its damage is the number of unordered pairs of servers that can reach each other now but could not after the failure. Return [i, damage] for the cable with the largest damage, choosing the smallest index i on ties. If no single failure disconnects anything, return [-1, 0].

Examples

Input:  n = 4, cables = [[0, 1], [1, 2], [2, 0], [2, 3]]
Output: [3, 3]
Explanation: only cable 3 matters; losing it cuts server 3 off from the other 3 servers.

Input:  n = 5, cables = [[0, 1], [1, 2], [2, 3], [3, 4]]
Output: [1, 6]
Explanation: cables 1 and 2 each split the chain into groups of 2 and 3 (6 pairs); 1 is smaller.

Input:  n = 3, cables = [[0, 1], [1, 0], [1, 2]]
Output: [2, 2]
Explanation: servers 0 and 1 are joined twice, so losing one of those cables changes nothing.

Constraints

  • 1 <= n <= 10**5, 0 <= len(cables) <= 1.2 * 10**5.
  • Target about O(n + len(cables)); removing each cable and re-searching is far too slow.

Goals

  • Find every bridge with discovery times and low-link values
  • Use DFS subtree sizes to count the pairs a bridge separates
  • Write the DFS with an explicit stack so 10**5 nodes do not overflow the call stack
Starting Python…