forked from namanj401/scaling-enigma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Critical Connextions.cpp
48 lines (48 loc) · 1.09 KB
/
Critical Connextions.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
vector<vector<int>> ans;
int vis[100003];
int tin[100003];
int low[100003];
int timer;
class Solution {
public:
/*
see cp-algo for explainations
*/
void dfs(vector<vector<int>> &adj,int x,int par=-1)
{
tin[x]=timer++;
low[x]=tin[x];
vis[x]=1;
for(auto j:adj[x])
{
if(j==par)
continue;
if(vis[j]==1)
{
low[x]=min(low[x],tin[j]);
}
else
{
dfs(adj,j,x);
low[x]=min(low[x],low[j]);
if(low[j]>tin[x])
ans.push_back({x,j});
}
}
}
vector<vector<int>> criticalConnections(int n, vector<vector<int>>& connections) {
vector<vector<int>> adj(n);
for(auto j:connections)
{
adj[j[0]].push_back(j[1]);
adj[j[1]].push_back(j[0]);
}
timer=0;
ans.clear();
memset(vis,0,sizeof(vis));
memset(tin,-1,sizeof(tin));
memset(low,-1,sizeof(low));
dfs(adj,0);
return ans;
}
};