-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridges_detection_undirected_graph.txt
57 lines (51 loc) · 1.69 KB
/
bridges_detection_undirected_graph.txt
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
class Solution {
public:
int timer;
vector<vector<int>> result;
void dfs(int parent, int grand_p, vector<int> &low, vector<int> &tin, vector<int> &visited, vector<vector<int>>& connections)
{
visited[parent] = 1;
++timer;
tin[parent] = timer;
low[parent] = timer;
for (int to : connections[parent]) {
if (to == grand_p)
continue;
if (visited[to] == 1) {
low[parent] = min(low[parent], tin[to]);
} else {
dfs(to, parent, low, tin, visited, connections);
low[parent] = min(low[parent], low[to]);
if (low[to] > tin[parent]) {
vector<int> temp;
temp.push_back(parent);
temp.push_back(to);
result.push_back(temp);
}
}
}
}
vector<vector<int>> criticalConnections(int n, vector<vector<int>>& connections) {
vector<int> low;
vector<int> tin;
vector<int> visited;
vector<vector<int>> adj_list;
adj_list.resize(n);
for (int i = 0; i < connections.size(); i++) {
adj_list[connections[i][0]].push_back(connections[i][1]);
adj_list[connections[i][1]].push_back(connections[i][0]);
}
timer = 0;
//low.assign(n, INT_MAX);
//tin.assign(n, INT_MAX);
low.resize(n);
tin.resize(n);
visited.resize(n);
for (int i = 0; i < n; i++) {
if (visited[i] == 0) {
dfs(i, -1, low, tin, visited, adj_list);
}
}
return result;
}
};