-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_1192_CriticalConnectionsInNetwork.cpp
58 lines (51 loc) · 1.34 KB
/
LC_1192_CriticalConnectionsInNetwork.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
1192. Critical Connections in a Network
https://leetcode.com/problems/critical-connections-in-a-network/
*/
class Solution {
int timer;
vector<int> dtime;
vector<int> low;
vector<bool> visited;
vector<vector<int>> ans;
vector<vector<int>> adj;
public:
vector<vector<int>> criticalConnections(int n, vector<vector<int>>& connections) {
adj.resize(n);
for(auto edge: connections)
{
int u=edge[0], v = edge[1];
adj[u].push_back(v);
adj[v].push_back(u);
}
timer=0;
dtime.resize(n, 0);
low.resize(n, 0);
visited.resize(n, false);
// for(int i=0; i<n; i++)
// {
// if(visited[i] == false)
dfs(0, -1);
// }
return ans;
}
void dfs(int s, int p)
{
dtime[s] = low[s] = timer++;
visited[s] = true;
for(auto &w: adj[s])
{
if(visited[w] ==false)
{
dfs(w, s);
low[s] = min(low[s], low[w]);
}
else if(w != p) // w visited and w not same as parent that is back edge
{
low[s] = min(low[s], dtime[w]);
}
if(low[w]>dtime[s])
ans.push_back({s, w});
}
}
};