forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum-path-quality-of-a-graph.cpp
135 lines (125 loc) · 4.58 KB
/
maximum-path-quality-of-a-graph.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
132
133
134
135
// Time: O(|V| + |E| + 4^(maxTime/min(times))) = O(|V| + |E| + 4^10)
// Space: O(|V| + |E|)
class Solution {
public:
int maximalPathQuality(vector<int>& values, vector<vector<int>>& edges, int maxTime) {
vector<vector<pair<int, int>>> adj(size(values));
for (const auto& edge : edges) {
adj[edge[0]].emplace_back(edge[1], edge[2]);
adj[edge[1]].emplace_back(edge[0], edge[2]);
}
return iter_dfs(values, adj, maxTime);
}
private:
int iter_dfs(const vector<int>& values,
const vector<vector<pair<int, int>>>& adj,
int maxTime) {
vector<int> lookup(size(values));
vector<unordered_set<int>> lookup2(size(values));
int result = 0;
vector<tuple<int, int, int, int, int>> stk = {{1, 0, -1, maxTime, 0}};
while (!empty(stk)) {
auto [step, u, v, time, total] = stk.back(); stk.pop_back();
if (step == 1) {
if (++lookup[u] == 1) {
total += values[u];
}
if (!u) {
result = max(result, total);
}
stk.emplace_back(4, u, -1, -1, -1);
for (const auto& [v, t] : adj[u]) {
if (lookup2[u].count(v) || time < t) { // same directed edge won't be visited twice
continue;
}
stk.emplace_back(3, u, v, -1, -1);
stk.emplace_back(1, v, -1, time - t, total);
stk.emplace_back(2, u, v, -1, -1);
}
} else if (step == 2) {
lookup2[u].emplace(v);
} else if (step == 3) {
lookup2[u].erase(v);
} else if (step == 4) {
--lookup[u];
}
}
return result;
}
};
// Time: O(|V| + |E| + 4^(maxTime/min(times))) = O(|V| + |E| + 4^10)
// Space: O(|V| + |E|)
class Solution2 {
public:
int maximalPathQuality(vector<int>& values, vector<vector<int>>& edges, int maxTime) {
vector<vector<pair<int, int>>> adj(size(values));
for (const auto& edge : edges) {
adj[edge[0]].emplace_back(edge[1], edge[2]);
adj[edge[1]].emplace_back(edge[0], edge[2]);
}
vector<int> lookup(size(values));
vector<unordered_set<int>> lookup2(size(values));
int result = 0;
dfs(values, adj, 0, maxTime, 0, &lookup, &lookup2, &result);
return result;
}
private:
void dfs(const vector<int>& values,
const vector<vector<pair<int, int>>>& adj,
int u, int time, int total,
vector<int> *lookup,
vector<unordered_set<int>> *lookup2,
int *result) {
if (++(*lookup)[u] == 1) {
total += values[u];
}
if (!u) {
*result = max(*result, total);
}
for (const auto& [v, t] : adj[u]) {
if ((*lookup2)[u].count(v) || time < t) { // same directed edge won't be visited twice
continue;
}
(*lookup2)[u].emplace(v);
dfs(values, adj, v, time - t, total, lookup, lookup2, result);
(*lookup2)[u].erase(v);
}
--(*lookup)[u];
}
};
// Time: O(|V| + |E| + 4^(maxTime/min(times))) = O(|V| + |E| + 4^10)
// Space: O(|V| + |E|)
class Solution3 {
public:
int maximalPathQuality(vector<int>& values, vector<vector<int>>& edges, int maxTime) {
vector<vector<pair<int, int>>> adj(size(values));
for (const auto& edge : edges) {
adj[edge[0]].emplace_back(edge[1], edge[2]);
adj[edge[1]].emplace_back(edge[0], edge[2]);
}
vector<int> lookup(size(values));
vector<unordered_set<int>> lookup2(size(values));
return dfs(values, adj, 0, maxTime, 0, &lookup, &lookup2);
}
private:
int dfs(const vector<int>& values,
const vector<vector<pair<int, int>>>& adj,
int u, int time, int total,
vector<int> *lookup,
vector<unordered_set<int>> *lookup2) {
if (++(*lookup)[u] == 1) {
total += values[u];
}
int result = !u ? total : 0;
for (const auto& [v, t] : adj[u]) {
if ((*lookup2)[u].count(v) || time < t) { // same directed edge won't be visited twice
continue;
}
(*lookup2)[u].emplace(v);
result = max(result, dfs(values, adj, v, time - t, total, lookup, lookup2));
(*lookup2)[u].erase(v);
}
--(*lookup)[u];
return result;
}
};