-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
minimum-edge-reversals-so-every-node-is-reachable.cpp
82 lines (76 loc) · 2.37 KB
/
minimum-edge-reversals-so-every-node-is-reachable.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
72
73
74
75
76
77
78
79
80
81
82
// Time: O(n)
// Space: O(n)
// iterative dfs, tree dp
class Solution {
public:
vector<int> minEdgeReversals(int n, vector<vector<int>>& edges) {
unordered_map<int, unordered_map<int, int>> adj;
for (const auto& e : edges) {
adj[e[0]][e[1]] = 0;
adj[e[1]][e[0]] = 1;
}
const auto& iter_dfs1 = [&]() {
int result = 0;
vector<pair<int, int>> stk = {{0, -1}};
while (!empty(stk)) {
const auto [u, p] = stk.back(); stk.pop_back();
for (const auto& [v, _] : adj[u]) {
if (v == p) {
continue;
}
result += adj[u][v];
stk.emplace_back(v, u);
}
}
return result;
};
const auto& iter_dfs2 = [&](int curr) {
vector<int> result(n, -1);
vector<pair<int, int>> stk = {{0, curr}};
while (!empty(stk)) {
const auto [u, curr] = stk.back(); stk.pop_back();
result[u] = curr;
for (const auto& [v, _] : adj[u]) {
if (result[v] == -1) {
stk.emplace_back(v, curr - adj[u][v] + adj[v][u]);
}
}
}
return result;
};
return iter_dfs2(iter_dfs1());
}
};
// Time: O(n)
// Space: O(n)
// dfs, tree dp
class Solution2 {
public:
vector<int> minEdgeReversals(int n, vector<vector<int>>& edges) {
unordered_map<int, unordered_map<int, int>> adj;
for (const auto& e : edges) {
adj[e[0]][e[1]] = 0;
adj[e[1]][e[0]] = 1;
}
const function<int (int, int)> dfs1 = [&](int u, int p) {
int result = 0;
for (const auto& [v, _] : adj[u]) {
if (v != p) {
result += adj[u][v] + dfs1(v, u);
}
}
return result;
};
vector<int> result(n, -1);
const function<void (int, int)> dfs2 = [&](int u, int curr) {
result[u] = curr;
for (const auto& [v, _] : adj[u]) {
if (result[v] == -1) {
dfs2(v, curr - adj[u][v] + adj[v][u]);
}
}
};
dfs2(0, dfs1(0, -1));
return result;
}
};