This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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];
ll solve(int v, int d) {
if (dp[v].find(d) != dp[v].end()) return dp[v][d];
ll a = 0, b = arr[v].second;
for (int i = 0; i < adj[v].size(); i++) {
int u = adj[v][i];
a += solve(u, d);
if (arr[v].first >= 1 && arr[v].first <= d) b += solve(u, arr[v].first);
}
if (arr[v].first >= 1 && arr[v].first <= d) return dp[v][d] = max(a, b);
else return dp[v][d] = a;
}
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};
}
cout << solve(1, k) << "\n";
return 0;
}
Compilation message (stderr)
magictree.cpp: In function 'll solve(int, int)':
magictree.cpp:13:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | for (int i = 0; i < adj[v].size(); i++) {
| ~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |