-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridgesInAGrapph.cpp
71 lines (65 loc) · 1.49 KB
/
bridgesInAGrapph.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
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>
using namespace std;
void dfs(int node, int par, vector<int> &id, vector<int> &low,
vector<int> &vis, unordered_map<int, vector<int>> &m,
vector<vector<int>> &ans,int &count)
{
vis[node] = 1;
id[node] = count;
low[node] = count;
count++;
for (auto i : m[node])
{
if (i == par)
{
continue;
}
if (!vis[i])
{
dfs(i, node, id, low, vis, m, ans,count);
low[node] = min(low[node], low[i]);
cout<<low[i]<<" "<< id[node]<<endl;
if (low[i] > id[node])
{
ans.push_back({node, i});
}
}
else
{
low[node] = min(low[node], low[i]);
}
}
}
vector<vector<int>> criticalConnections(int n, vector<vector<int>> &connections)
{
unordered_map<int, vector<int>> m;
for (auto i : connections)
{
m[i[0]].push_back(i[1]);
m[i[1]].push_back(i[0]);
}
vector<int> id(n, 0);
vector<int> low(n, 0);
vector<int> vis(n, 0);
int count=0;
vector<vector<int>> ans;
for (int i = 0; i < n; i++)
{
if (!vis[i])
{
dfs(i, -1, id, low, vis, m, ans,count);
}
}
return ans;
}
int main()
{
vector<vector<int>> v={{0,1},{1,2},{2,0},{1,3}};
vector<vector<int>> temp=criticalConnections(4,v);
for(auto i:temp){
for(auto j:i){
cout<<j<<" ";
}
cout<<endl;
}
}