forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreorder-routes-to-make-all-paths-lead-to-the-city-zero.cpp
56 lines (53 loc) · 1.8 KB
/
reorder-routes-to-make-all-paths-lead-to-the-city-zero.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
// Time: O(n)
// Space: O(n)
class Solution {
public:
int minReorder(int n, vector<vector<int>>& connections) {
unordered_set<uint64_t> lookup;
unordered_map<int, vector<int>> graph;
for (const auto& connection : connections) {
lookup.emplace(uint64_t(connection[0]) * n + connection[1]);
graph[connection[0]].emplace_back(connection[1]);
graph[connection[1]].emplace_back(connection[0]);
}
int result = 0;
vector<pair<int, int>> stk = {{-1, 0}};
while (!stk.empty()) {
const auto [parent, u] = stk.back(); stk.pop_back();
for (const auto& v : graph[u]) {
if (v == parent) {
continue;
}
stk.emplace_back(u, v);
}
result += lookup.count(uint64_t(parent) * n + u);
}
return result;
}
};
// Time: O(n)
// Space: O(n)
class Solution2 {
public:
int minReorder(int n, vector<vector<int>>& connections) {
unordered_set<uint64_t> lookup;
unordered_map<int, vector<int>> graph;
for (const auto& connection : connections) {
lookup.emplace(uint64_t(connection[0]) * n + connection[1]);
graph[connection[0]].emplace_back(connection[1]);
graph[connection[1]].emplace_back(connection[0]);
}
return dfs(n, lookup, graph, -1, 0);
}
private:
int dfs(int n, const unordered_set<uint64_t>& lookup, const unordered_map<int, vector<int>>& graph, int parent, int u) {
int result = lookup.count(uint64_t(parent) * n + u);
for (const auto& v : graph.at(u)) {
if (v == parent) {
continue;
}
result += dfs(n, lookup, graph, u, v);
}
return result;
}
};