forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-time-to-collect-all-apples-in-a-tree.cpp
131 lines (125 loc) · 4.38 KB
/
minimum-time-to-collect-all-apples-in-a-tree.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Time: O(n)
// Space: O(n)
class Solution {
public:
int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {
unordered_map<int, vector<int>> graph;
for (const auto& edge : edges) {
graph[edge[0]].emplace_back(edge[1]);
graph[edge[1]].emplace_back(edge[0]);
}
using RET = pair<int, int>;
RET result{};
vector<tuple<int, int, int, shared_ptr<RET>, RET *>> stk = {{1, -1, 0, nullptr, &result}};
while (!stk.empty()) {
const auto [step, par, node, new_ret, ret] = stk.back(); stk.pop_back();
if (step == 1) {
ret->second = int(hasApple[node]);
for (const auto& nei : graph[node]) {
if (nei == par) {
continue;
}
const auto& new_ret = make_shared<RET>();
stk.emplace_back(2, -1, -1, new_ret, ret);
stk.emplace_back(1, node, nei, nullptr, new_ret.get());
}
} else {
ret->first += new_ret->first + new_ret->second;
ret->second |= bool(new_ret->first + new_ret->second);
}
}
return 2 * result.first;
}
};
// Time: O(n)
// Space: O(n)
class Solution_Recu {
public:
int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {
unordered_map<int, vector<int>> graph;
for (const auto& edge : edges) {
graph[edge[0]].emplace_back(edge[1]);
graph[edge[1]].emplace_back(edge[0]);
}
return 2 * dfs(graph, -1, 0, hasApple).first;
}
private:
pair<int, int> dfs(const unordered_map<int, vector<int>>& graph,
int par, int node,
const vector<bool>& hasApple) {
int result = 0, extra = hasApple[node];
for (const auto& nei : graph.at(node)) {
if (nei == par) {
continue;
}
const auto& [count, found] = dfs(graph, node, nei, hasApple);
result += count + found;
extra |= bool(count + found);
}
return {result, extra};
}
};
// Time: O(n)
// Space: O(n)
class Solution2 {
public:
int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {
unordered_map<int, vector<int>> graph;
for (const auto& edge : edges) {
graph[edge[0]].emplace_back(edge[1]);
graph[edge[1]].emplace_back(edge[0]);
}
using RET = int;
RET result{};
vector<tuple<int, int, int, shared_ptr<RET>, shared_ptr<RET>, RET *>> stk = {{1, -1, 0, nullptr, nullptr, &result}};
while (!stk.empty()) {
const auto [step, par, node, new_ret, tmp, ret] = stk.back(); stk.pop_back();
if (step == 1) {
const auto& tmp = make_shared<RET>(hasApple[node]);
stk.emplace_back(3, -1, -1, nullptr, tmp, ret);
for (const auto& nei : graph[node]) {
if (nei == par) {
continue;
}
const auto& new_ret = make_shared<RET>();
stk.emplace_back(2, -1, -1, new_ret, tmp, ret);
stk.emplace_back(1, node, nei, nullptr, nullptr, new_ret.get());
}
} else if (step == 2) {
*ret += *new_ret;
*tmp |= bool(*new_ret);
} else {
*ret += *tmp;
}
}
return 2 * max(result - 1, 0);
}
};
// Time: O(n)
// Space: O(n)
class Solution2_Recu {
public:
int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {
unordered_map<int, vector<int>> graph;
for (const auto& edge : edges) {
graph[edge[0]].emplace_back(edge[1]);
graph[edge[1]].emplace_back(edge[0]);
}
return 2 * max(dfs(graph, -1, 0, hasApple) - 1, 0);
}
private:
int dfs(const unordered_map<int, vector<int>>& graph,
int par, int node,
const vector<bool>& hasApple) {
int result = 0, extra = hasApple[node];
for (const auto& nei : graph.at(node)) {
if (nei == par) {
continue;
}
const auto& count = dfs(graph, node, nei, hasApple);
result += count;
extra |= bool(count);
}
return result + extra;
}
};