Lv.4bridgesdfsgraph
Critical Roads (Bridges)
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Road i is roads[i] = [a, b] (two-way). A road is critical if removing it disconnects two cities that were connected.
Return the indices of the critical roads in increasing order. Parallel roads protect each other.
Examples
| n | roads | result |
|---|---|---|
5 | [[0, 1], [1, 2], [2, 0], [1, 3], [3, 4]] | [3, 4] |
2 | [[0, 1], [1, 0]] | [] |
4 | [[0, 1], [2, 3]] | [0, 1] |
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(n: int, roads: int[][]) → int[]
Constraints
-
n,roads.length - ; parallel roads may appear.
Expected complexity
- Time
- O(N + M)
- Space
- O(N + M)
Examples
| n | roads | Expected |
|---|---|---|
5 | [[0,1],[1,2],[2,0],[1,3],[3,4]] | [3,4] |
2 | [[0,1],[1,0]] | [] |
4 | [[0,1],[2,3]] | [0,1] |
Hint
Hint
Tarjan's bridge finding: a tree edge u → v is a bridge iff low[v] > tin[u]. Skip the parent by edge index (parallel
roads), and use an iterative DFS for deep graphs. Removing each road and re-checking connectivity is .
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1738 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 8 | verdict only |
| performance | 60 | 4 | verdict only |