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>
//define endl '\n'
using namespace std;
const int MAXN = 2e5 + 10, MAXK = 22;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int n, m, k;
int p[MAXN];
long long v[MAXN], d[MAXN], w[MAXN];
vector < int > g[MAXN];
pair < int, int > f[MAXN];
long long leaf[MAXN], total = 0;
void read()
{
cin >> n >> m >> k;
for (int i = 1; i <= n; ++ i)
leaf[i] = 1;
for (int i = 2; i <= n; ++ i)
{
cin >> p[i];
g[p[i]].push_back(i);
leaf[p[i]] = 1;
}
bool no = 0;
for (int i = 1; i <= m; ++ i)
{
cin >> v[i] >> d[i] >> w[i];
total += w[i];
f[v[i]] = make_pair(d[i], w[i]);
}
}
bool cmp(pair < int, int > p1, pair < int, int > p2)
{
return (p1.first > p2.first);
}
long long sub[MAXN][MAXK], dp[MAXN][MAXK], pref[MAXN][MAXK];
void dfs(int beg, int par)
{
int dd = f[beg].first;
if(dd)sub[beg][dd] += f[beg].second;
long long sum = 0;
for (auto nb: g[beg])
{
if(nb == par)continue;
dfs(nb, beg);
for (int i = 1; i <= k; ++ i)
sub[beg][i] += sub[nb][i];
}
for (int from = 1; from <= k; ++ from)
{
for (int to = 1; to <= k; ++ to)
{
dp[beg][to] = max(dp[beg][to], sub[beg][from] - (sub[beg][to]));
}
}
for (int day = 1; day <= k; ++ day)
{
long long cost = 0;
cost += sub[beg][day];
long long sum = 0;
for (auto nb: g[beg])
sum += dp[nb][day];
cost += sum;
dp[beg][day] = max(dp[beg][day], cost);
pref[beg][day] = max(dp[beg][day], dp[beg][day-1]);
}
}
int main()
{
speed();
read();
vector < pair < int, int > > f;
vector < pair < int, int > > s;
long long ans = 0;
for (auto x: g[1])
{
dfs(x, 1);
ans = ans + dp[x][k];
}
cout << ans << endl;
return 0;
}
/***
7 5 2
1 2 2 1 4 4
2 2 1
3 2 1
4 1 1
6 1 1
5 1 10
*/
Compilation message (stderr)
magictree.cpp: In function 'void read()':
magictree.cpp:29:10: warning: unused variable 'no' [-Wunused-variable]
29 | bool no = 0;
| ^~
magictree.cpp: In function 'void dfs(int, int)':
magictree.cpp:49:15: warning: unused variable 'sum' [-Wunused-variable]
49 | long long sum = 0;
| ^~~
# | 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... |