forked from Masked-coder11/gfg-POTD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.02.2024.cpp
53 lines (41 loc) · 1.24 KB
/
14.02.2024.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
//User function Template for C++
class Solution {
public:
vector<vector<int>>ans;
int time=0;
void dfs(int u, vector<int>&tin, vector<int>&lowest, vector<int>&parent, vector<int>adj[]){
tin[u] = lowest[u] =time++;
for(int i=0;i<adj[u].size();i++){
int v=adj[u][i];
if(tin[v]==-1){
parent[v]=u;
dfs(v, tin, lowest, parent, adj);
if(lowest[v] > tin[u]){
ans.push_back({v,u});
}
else{
lowest[u]=min(lowest[u], lowest[v]);
}
}
else if(tin[v]>-1 && v!=parent[u]){
lowest[u] = min(lowest[u], lowest[v]);
}
}
}
vector<vector<int>>criticalConnections(int v, vector<int> adj[]){
// Code here
vector<int>tin(v,-1);
vector<int>lowest(v,-1);
vector<int>parent(v,-1);
for(int i=0;i<v;i++){
if(tin[i]==-1){
dfs(i, tin, lowest, parent, adj);
}
}
for(int i=0;i<ans.size();i++){
sort(ans[i].begin(), ans[i].end());
}
sort(ans.begin(), ans.end());
return ans;
}
};