-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
34 lines (29 loc) · 1011 Bytes
/
main.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
// https://youtu.be/TyWtx7q2D7Y
// https://youtu.be/mKUsbABiwBI
class Solution {
public:
vector<vector<int>> criticalConnections(int n,
vector<vector<int>>& connections) {
vector<vector<int>> adj(n);
for (auto c : connections) {
adj[c[0]].push_back(c[1]);
adj[c[1]].push_back(c[0]);
}
vector<vector<int>> result;
vector<int> vis(n, -1);
function<int(int, int, int)> dfs = [&](int u, int p, int rank) -> int {
if (vis[u] != -1) return vis[u];
vis[u] = rank;
int min_rank = rank;
for (int v : adj[u]) {
if (v == p) continue;
int returned_rank = dfs(v, u, rank + 1);
if (returned_rank == rank + 1) result.push_back({u, v});
min_rank = min(min_rank, returned_rank);
}
return min_rank;
};
dfs(0, -1, 0);
return result;
}
};