-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathmaximum-good-subtree-score.cpp
More file actions
130 lines (122 loc) · 4.19 KB
/
maximum-good-subtree-score.cpp
File metadata and controls
130 lines (122 loc) · 4.19 KB
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
// Time: O(n * (2^10)^2)
// Space: O(2^10)
// bitmasks, iterative dfs, tree dp
class Solution {
public:
int goodSubtreeSum(vector<int>& vals, vector<int>& par) {
static const int MOD = 1e9 + 7;
vector<vector<int>> adj(size(vals));
const auto& get_mask = [](int x) {
int mask = 0;
for (; x; x /= 10) {
const int d = x % 10;
if (mask & (1 << d)) {
return -1;
}
mask |= 1 << d;
}
return mask;
};
const auto& iter_dfs = [&]() {
int result = 0;
using RET = unordered_map<int, int>;
RET ret;
vector<tuple<int, int, int, shared_ptr<RET>, RET *>> stk = {{1, 0, -1, nullptr, &ret}};
while (!empty(stk)) {
const auto [step, u, i, new_ret, ret] = stk.back(); stk.pop_back();
if (step == 1) {
(*ret)[0] = 0;
const auto& mask = get_mask(vals[u]);
if (mask != -1) {
(*ret)[mask] = vals[u];
}
stk.emplace_back(4, u, -1, nullptr, ret);
stk.emplace_back(2, u, 0, nullptr, ret);
} else if (step == 2) {
if (i == size(adj[u])) {
continue;
}
const auto& v = adj[u][i];
stk.emplace_back(2, u, i + 1, nullptr, ret);
const auto& new_ret = make_shared<RET>();
stk.emplace_back(3, -1, -1, new_ret, ret);
stk.emplace_back(1, v, -1, nullptr, new_ret.get());
} else if (step == 3) {
unordered_map<int, int> copy_dp(*ret);
for (const auto& [m1, v1] : copy_dp) {
for (const auto& [m2, v2] : *new_ret) {
if (m1 & m2) {
continue;
}
(*ret)[m1 | m2] = max((*ret)[m1 | m2], v1 + v2);
}
}
} else if (step == 4) {
int mx = 0;
for (const auto& [_, v] : *ret) {
mx = max(mx, v);
}
result = (result + mx) % MOD;
}
}
return result;
};
for (int u = 1; u < size(par); ++u) {
adj[par[u]].emplace_back(u);
}
return iter_dfs();
}
};
// Time: O(n * (2^10)^2)
// Space: O(2^10)
// bitmasks, dfs, tree dp
class Solution2 {
public:
int goodSubtreeSum(vector<int>& vals, vector<int>& par) {
static const int MOD = 1e9 + 7;
int result = 0;
vector<vector<int>> adj(size(vals));
const auto& get_mask = [](int x) {
int mask = 0;
for (; x; x /= 10) {
const int d = x % 10;
if (mask & (1 << d)) {
return -1;
}
mask |= 1 << d;
}
return mask;
};
const function<unordered_map<int, int> (int)> dfs = [&](int u) {
unordered_map<int, int> dp;
dp[0] = 0;
const auto& mask = get_mask(vals[u]);
if (mask != -1) {
dp[mask] = vals[u];
}
for (const auto& v : adj[u]) {
const auto& new_dp = dfs(v);
unordered_map<int, int> copy_dp(dp);
for (const auto& [m1, v1] : copy_dp) {
for (const auto& [m2, v2] : new_dp) {
if (m1 & m2) {
continue;
}
dp[m1 | m2] = max(dp[m1 | m2], v1 + v2);
}
}
}
int mx = 0;
for (const auto& [_, v] : dp) {
mx = max(mx, v);
}
result = (result + mx) % MOD;
return dp;
};
for (int u = 1; u < size(par); ++u) {
adj[par[u]].emplace_back(u);
}
dfs(0);
return result;
}
};