-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1192.cpp
More file actions
40 lines (38 loc) · 1.02 KB
/
1192.cpp
File metadata and controls
40 lines (38 loc) · 1.02 KB
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
33
34
35
36
37
38
39
40
class Solution {
public:
vector<bool> visited;
vector<int> tin,low;
vector<vector<int>> adjList;
vector<vector<int>> res;
int timer;
void dfs(int u, int parent){
visited[u] = true;
tin[u] = low[u] = timer++;
for(auto v: adjList[u]){
if(v==parent)
continue;
if(visited[v])
low[u] = min(low[u],tin[v]);
else{
dfs(v,u);
low[u] = min(low[u],low[v]);
if(low[v]>tin[u]){
res.push_back({u,v});
}
}
}
}
vector<vector<int>> criticalConnections(int n, vector<vector<int>>& connections) {
timer = 0;
adjList.resize(n);
visited.resize(n,0);
tin.resize(n,-1);
low.resize(n,-1);
for(auto edge: connections){//O(E)
adjList[edge[0]].push_back(edge[1]);
adjList[edge[1]].push_back(edge[0]);
}
dfs(0,-1);
return res;
}
};