Tarjan bridges
Time O(V + E) Space O(V + E)One DFS computing disc and low; skip the edge back to the parent; record bridges.
n = 4, connections = [[0,1],[1,2],[2,0],[1,3]]disc / low(map)
Step 1/4DFS visits 0, then 1, then 2, stamping visit times 0, 1, 2.
import java.util.*;
class Solution {
private List<List<Integer>> adj;
private int[] disc, low;
private int time = 0;
private final List<List<Integer>> bridges = new ArrayList<>();
public List<List<Integer>> criticalConnections(int n, List<List<Integer>> connections) {
adj = new ArrayList<>();
for (int i = 0; i < n; i++) adj.add(new ArrayList<>());
for (List<Integer> e : connections) { adj.get(e.get(0)).add(e.get(1)); adj.get(e.get(1)).add(e.get(0)); }
disc = new int[n];
low = new int[n];
Arrays.fill(disc, -1);
dfs(0, -1);
return bridges;
}
private void dfs(int u, int parent) {
disc[u] = low[u] = time++;
for (int v : adj.get(u)) {
if (v == parent) continue;
if (disc[v] == -1) {
dfs(v, u);
low[u] = Math.min(low[u], low[v]);
if (low[v] > disc[u]) bridges.add(List.of(u, v));
} else {
low[u] = Math.min(low[u], disc[v]);
}
}
}
}Verdict: Linear; trying each edge removal would be O(E × (V + E)).