1115. Separate the Servers

Hard7.5 points estimatedGraphBreadth-First Search3 hints

A data centre contains n servers numbered 0 to n - 1. Each entry [a, b] of edges is a network cable that carries traffic in both directions between servers a and b. Two servers may be joined by several cables, and a cable may even loop from a server back to itself.

During maintenance, an engineer wants the server source to be completely unable to talk to the server sink, directly or through any chain of other servers. Return the minimum number of cables that must be unplugged to achieve this. If the two servers are already unable to communicate, return 0. Every cable counts separately, even when it duplicates another one.

Example 1

Input:
n = 5edges = [[0,1],[0,2],[1,3],[2,3],[3,4],[1,2]]source = 0sink = 4
Output:
1
Explanation:

Every route must use the single cable 3-4 to reach server 4, so unplugging that one cable is enough.

Example 2

Input:
n = 4edges = [[0,1],[0,1],[0,2],[1,3],[2,3],[2,3]]source = 0sink = 3
Output:
2
Explanation:

Unplugging cables 0-2 and 1-3 splits servers {0, 1} from {2, 3}, and one cable alone cannot do it, so the answer is 2.

Example 3

Input:
n = 4edges = [[0,1],[2,3]]source = 0sink = 3
Output:
0
Explanation:

Servers 0 and 3 are in different groups already, so nothing has to be unplugged.

Constraints

2 ≤ n ≤ 3000

0 ≤ edges.length ≤ 24000, each edge is [a, b] with 0 ≤ a, b < n

0 ≤ source, sink < n and source ≠ sink

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(cuts * E)
Space
O(V + E)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…