#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int n, m, k, par[101010];
pii arr[101010];
vector<int> adj[101010];
map<int, ll> dp[101010];
void solve(int v) {
for (int i = 0; i < adj[v].size(); i++) {
int u = adj[v][i];
solve(u);
if (dp[v].size() < dp[u].size()) swap(dp[v], dp[u]);
for (auto iter = dp[u].begin(); iter != dp[u].end(); iter++) {
dp[v][iter->first] += iter->second;
}
}
dp[v][arr[v].first] += arr[v].second;
auto iter = dp[v].upper_bound(arr[v].first);
while (iter != dp[v].end() && arr[v].second > 0) {
if (iter->second > arr[v].second) {
iter->second -= arr[v].second;
arr[v].second = 0;
}
else {
arr[v].second -= iter->second;
iter = dp[v].erase(iter);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> k;
for (int i = 2; i <= n; i++) {
cin >> par[i];
adj[par[i]].push_back(i);
}
for (int i = 1; i <= m; i++) {
int v, d, w;
cin >> v >> d >> w;
arr[v] = {d, w};
}
solve(1);
ll ans = 0;
for (auto iter = dp[1].begin(); iter != dp[1].end(); iter++) {
ans += iter->second;
}
cout << ans << "\n";
return 0;
}
Compilation message
magictree.cpp: In function 'void solve(int)':
magictree.cpp:11:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
11 | for (int i = 0; i < adj[v].size(); i++) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
7512 KB |
Output is correct |
5 |
Correct |
2 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
2 ms |
7580 KB |
Output is correct |
8 |
Correct |
2 ms |
7512 KB |
Output is correct |
9 |
Correct |
2 ms |
7512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
20676 KB |
Output is correct |
2 |
Correct |
31 ms |
18524 KB |
Output is correct |
3 |
Correct |
103 ms |
35500 KB |
Output is correct |
4 |
Correct |
55 ms |
18896 KB |
Output is correct |
5 |
Correct |
85 ms |
20360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
3 ms |
7776 KB |
Output is correct |
4 |
Correct |
42 ms |
26352 KB |
Output is correct |
5 |
Correct |
56 ms |
30036 KB |
Output is correct |
6 |
Correct |
50 ms |
26116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
14672 KB |
Output is correct |
2 |
Correct |
36 ms |
13916 KB |
Output is correct |
3 |
Correct |
42 ms |
17516 KB |
Output is correct |
4 |
Correct |
29 ms |
15320 KB |
Output is correct |
5 |
Correct |
35 ms |
24348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
7512 KB |
Output is correct |
5 |
Correct |
2 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
2 ms |
7580 KB |
Output is correct |
8 |
Correct |
2 ms |
7512 KB |
Output is correct |
9 |
Correct |
2 ms |
7512 KB |
Output is correct |
10 |
Correct |
48 ms |
17492 KB |
Output is correct |
11 |
Correct |
46 ms |
18000 KB |
Output is correct |
12 |
Correct |
35 ms |
18852 KB |
Output is correct |
13 |
Correct |
32 ms |
16528 KB |
Output is correct |
14 |
Correct |
36 ms |
25688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8796 KB |
Output is correct |
2 |
Correct |
23 ms |
13996 KB |
Output is correct |
3 |
Correct |
24 ms |
14172 KB |
Output is correct |
4 |
Correct |
23 ms |
14464 KB |
Output is correct |
5 |
Correct |
14 ms |
15580 KB |
Output is correct |
6 |
Correct |
24 ms |
16912 KB |
Output is correct |
7 |
Correct |
21 ms |
18012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
7512 KB |
Output is correct |
5 |
Correct |
2 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
2 ms |
7580 KB |
Output is correct |
8 |
Correct |
2 ms |
7512 KB |
Output is correct |
9 |
Correct |
2 ms |
7512 KB |
Output is correct |
10 |
Correct |
2 ms |
7516 KB |
Output is correct |
11 |
Correct |
2 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7776 KB |
Output is correct |
13 |
Correct |
42 ms |
26352 KB |
Output is correct |
14 |
Correct |
56 ms |
30036 KB |
Output is correct |
15 |
Correct |
50 ms |
26116 KB |
Output is correct |
16 |
Correct |
48 ms |
17492 KB |
Output is correct |
17 |
Correct |
46 ms |
18000 KB |
Output is correct |
18 |
Correct |
35 ms |
18852 KB |
Output is correct |
19 |
Correct |
32 ms |
16528 KB |
Output is correct |
20 |
Correct |
36 ms |
25688 KB |
Output is correct |
21 |
Correct |
14 ms |
11100 KB |
Output is correct |
22 |
Correct |
58 ms |
21968 KB |
Output is correct |
23 |
Correct |
68 ms |
24852 KB |
Output is correct |
24 |
Correct |
94 ms |
31684 KB |
Output is correct |
25 |
Correct |
59 ms |
20360 KB |
Output is correct |
26 |
Correct |
73 ms |
22232 KB |
Output is correct |
27 |
Correct |
59 ms |
20724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
7512 KB |
Output is correct |
5 |
Correct |
2 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
2 ms |
7580 KB |
Output is correct |
8 |
Correct |
2 ms |
7512 KB |
Output is correct |
9 |
Correct |
2 ms |
7512 KB |
Output is correct |
10 |
Correct |
52 ms |
20676 KB |
Output is correct |
11 |
Correct |
31 ms |
18524 KB |
Output is correct |
12 |
Correct |
103 ms |
35500 KB |
Output is correct |
13 |
Correct |
55 ms |
18896 KB |
Output is correct |
14 |
Correct |
85 ms |
20360 KB |
Output is correct |
15 |
Correct |
2 ms |
7516 KB |
Output is correct |
16 |
Correct |
2 ms |
7516 KB |
Output is correct |
17 |
Correct |
3 ms |
7776 KB |
Output is correct |
18 |
Correct |
42 ms |
26352 KB |
Output is correct |
19 |
Correct |
56 ms |
30036 KB |
Output is correct |
20 |
Correct |
50 ms |
26116 KB |
Output is correct |
21 |
Correct |
40 ms |
14672 KB |
Output is correct |
22 |
Correct |
36 ms |
13916 KB |
Output is correct |
23 |
Correct |
42 ms |
17516 KB |
Output is correct |
24 |
Correct |
29 ms |
15320 KB |
Output is correct |
25 |
Correct |
35 ms |
24348 KB |
Output is correct |
26 |
Correct |
48 ms |
17492 KB |
Output is correct |
27 |
Correct |
46 ms |
18000 KB |
Output is correct |
28 |
Correct |
35 ms |
18852 KB |
Output is correct |
29 |
Correct |
32 ms |
16528 KB |
Output is correct |
30 |
Correct |
36 ms |
25688 KB |
Output is correct |
31 |
Correct |
6 ms |
8796 KB |
Output is correct |
32 |
Correct |
23 ms |
13996 KB |
Output is correct |
33 |
Correct |
24 ms |
14172 KB |
Output is correct |
34 |
Correct |
23 ms |
14464 KB |
Output is correct |
35 |
Correct |
14 ms |
15580 KB |
Output is correct |
36 |
Correct |
24 ms |
16912 KB |
Output is correct |
37 |
Correct |
21 ms |
18012 KB |
Output is correct |
38 |
Correct |
14 ms |
11100 KB |
Output is correct |
39 |
Correct |
58 ms |
21968 KB |
Output is correct |
40 |
Correct |
68 ms |
24852 KB |
Output is correct |
41 |
Correct |
94 ms |
31684 KB |
Output is correct |
42 |
Correct |
59 ms |
20360 KB |
Output is correct |
43 |
Correct |
73 ms |
22232 KB |
Output is correct |
44 |
Correct |
59 ms |
20724 KB |
Output is correct |
45 |
Correct |
16 ms |
11448 KB |
Output is correct |
46 |
Correct |
57 ms |
22644 KB |
Output is correct |
47 |
Correct |
71 ms |
26196 KB |
Output is correct |
48 |
Correct |
101 ms |
34548 KB |
Output is correct |
49 |
Correct |
57 ms |
21148 KB |
Output is correct |
50 |
Correct |
74 ms |
22868 KB |
Output is correct |
51 |
Correct |
62 ms |
21668 KB |
Output is correct |