/**
* author: wxhtzdy
* created: 26.04.2023 14:44:59
**/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
vector<vector<int>> g(n);
for (int i = 1; i < n; i++) {
int p;
cin >> p;
--p;
g[p].push_back(i);
g[i].push_back(p);
}
vector<int> a(n, -1), b(n);
for (int i = 0; i < m; i++) {
int v, d, w;
cin >> v >> d >> w;
--v; --d;
a[v] = d;
b[v] = w;
}
vector<int> rt(n);
vector<vector<pair<int, int>>> vals(n);
function<void(int, int)> Dfs = [&](int v, int pv) {
int f = -1;
for (int u : g[v]) {
if (u == pv) {
continue;
}
Dfs(u, v);
if (f == -1 || vals[rt[f]].size() < vals[rt[u]].size()) {
f = u;
}
}
rt[v] = v;
for (int u : g[v]) {
if (u == pv) {
continue;
}
for (auto& p : vals[rt[u]]) {
vals[rt[v]].push_back(p);
}
}
sort(vals[rt[v]].begin(), vals[rt[v]].end());
if (a[v] != -1) {
vals[rt[v]].emplace_back(a[v], b[v]);
for (auto& p : vals[rt[v]]) {
if (p.first > a[v]) {
vals[rt[v]].emplace_back(p.first, -b[v]);
break;
}
}
}
};
Dfs(0, 0);
sort(vals[rt[0]].begin(), vals[rt[0]].end());
long long ans = 0;
long long sum = 0;
for (auto& p : vals[rt[0]]) {
sum += p.second;
ans = max(ans, sum);
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
13540 KB |
Output is correct |
2 |
Execution timed out |
2115 ms |
633264 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
5700 KB |
Output is correct |
2 |
Incorrect |
21 ms |
5460 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
142 ms |
23780 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
2260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |