Depth first search · Hard
A service mesh must identify individual undirected links whose loss would disconnect part of the current network.
Return every bridge edge by its original input index.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ n:integer, links:[u:integer,v:integer][] }
Input rules
- Parallel links and self-links are allowed.
What each field means
n— number of nodes, identified by0throughn - 1.links— undirected[u, v]edges. Each edge is identified by its zero-based position in this array.
Output
Return value and rules
- Return indexes of edges whose individual removal increases the number of connected components.
- Return indexes in increasing order. Parallel edges remain distinct edges.
- Check all components even if the original graph is already disconnected.
Constraints
- 0 <= n <= 2000; 0 <= links.length <= 20000
- All endpoints valid. Input may already be disconnected; evaluate each connected component.
Examples
Example 1
{
"links": [
[
0,
1
],
[
1,
2
],
[
2,
0
],
[
2,
3
]
],
"n": 4
}Returns
[
3
]Example 2
{
"links": [],
"n": 0
}Returns
[]Required complexity
O(n + m) time and O(n + m) space