-
Notifications
You must be signed in to change notification settings - Fork 3
/
1192.cpp
32 lines (30 loc) · 1.1 KB
/
1192.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public:
vector<vector<int>> res;
void dfs(vector<vector<int>>& graph, vector<int>& low, vector<int>& time, int curr, int prev, int currTime) {
low[curr] = time[curr] = currTime;
for (auto neighbor : graph[curr]) {
if (time[neighbor] == 0) {
dfs(graph, low, time, neighbor, curr, currTime + 1);
low[curr] = min(low[curr], low[neighbor]);
}
else if (neighbor != prev) {
low[curr] = min(low[curr], low[neighbor]);
}
if (low[neighbor] > currTime) {
res.push_back({curr, neighbor});
}
}
}
vector<vector<int>> criticalConnections(int n, vector<vector<int>>& connections) {
vector<vector<int>> graph(n);
vector<int> low(n, 0);
vector<int> time(n, 0);
for (auto connection : connections) {
graph[connection[0]].push_back(connection[1]);
graph[connection[1]].push_back(connection[0]);
}
dfs(graph, low, time, 0, -1, 1);
return res;
}
};