-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1192.) Critical Connections in a Network.cpp
42 lines (37 loc) · 1.5 KB
/
1192.) Critical Connections in a Network.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
33
34
35
36
37
38
39
40
41
42
class Solution {
int time = 1;
public:
vector<vector<int>> criticalConnections(int n, vector<vector<int>>& connections) {
vector<vector<int>> adj(n);
vector<bool> visited(n, false);
vector<int> insertionTime(n), lowTime(n);
vector<vector<int>> bridges;
for(int i=0; i<connections.size(); i++)
{
adj[connections[i][0]].push_back(connections[i][1]);
adj[connections[i][1]].push_back(connections[i][0]);
}
dfs(0, -1, visited, adj, insertionTime, lowTime, bridges);
return bridges;
}
void dfs(int current, int parent, vector<bool>& visited, vector<vector<int>>& adj, vector<int>& insertionTime, vector<int>& lowTime, vector<vector<int>>& bridges) {
visited[current] = true;
insertionTime[current] = lowTime[current] = time;
time++;
for(int i=0; i<adj[current].size(); i++)
{
if(adj[current][i] != parent)
{
if(!visited[adj[current][i]])
{
dfs(adj[current][i], current, visited, adj, insertionTime, lowTime, bridges);
lowTime[current] = min(lowTime[current], lowTime[adj[current][i]]);
if(lowTime[adj[current][i]] > insertionTime[current])
bridges.push_back({ current, adj[current][i]});
}
else
lowTime[current] = min(lowTime[current], lowTime[adj[current][i]]);
}
}
}
};