-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlintcode1271.cpp
69 lines (61 loc) · 2.08 KB
/
lintcode1271.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
// https://youtu.be/ysro7IPHukk
class Solution {
public:
/**
* @param n: the number of servers
* @param connections: connections
* @return: Critical Connections in a Network
* trajan algorithm
* definition of vector<int> depth is important
* depth[i] means the minimum depth visited from node i
* so depth[i] is different from cur_depth
*/
int dfs(int cur,
int prev,
int cur_depth,
vector<int> &depth,
vector<vector<int>> &conn,
vector<vector<int>> &res) {
// starting from current node
// return the minimum depth
depth[cur] = cur_depth + 1;
for(int neighbor : conn[cur]) {
// don't go back to the same path
if(neighbor == prev)
continue;
if(depth[neighbor] == -1) {
// still not visited
// if we visit a node with less depth, then update
depth[cur] = min(depth[cur], dfs(neighbor, cur, cur_depth + 1, depth, conn, res));
}else {
// visited
depth[cur] = min(depth[cur], depth[neighbor]);
}
}
// if the return depth from neighbor is still
if(depth[cur] == cur_depth + 1 && cur != 0) {
if(cur < prev)
res.push_back({cur, prev});
else
res.push_back({prev, cur});
}
return depth[cur];
}
vector<vector<int>> criticalConnectionsinaNetwork(int n, vector<vector<int>> &connections) {
// write your code here
vector<vector<int>> res;
if(n == 1)
return {{0, 1}};
vector<vector<int>> conn(n, vector<int>());
for(auto i : connections) {
conn[i[0]].push_back(i[1]);
conn[i[1]].push_back(i[0]);
}
vector<int> depth(n, -1);
dfs(0, -1, -1, depth, conn, res);
for(auto i : depth) {
cout << i << '\n';
}
return res;
}
};