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;
long long maxon[MAXN];
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]);
}
if(!no)
{
cout << total << endl;
exit(0);
}
}
bool cmp(pair < int, int > p1, pair < int, int > p2)
{
return (p1.first > p2.first);
}
long long sub[MAXN][MAXK], dp[MAXN];
void dfs(int beg, int par)
{
sub[beg][1] = 0;
sub[beg][2] = 0;
if(f[beg].first == 1)sub[beg][1] += f[beg].second;
else if(f[beg].first == 2)sub[beg][2] += f[beg].second;
long long sum = 0;
for (auto nb: g[beg])
{
if(nb == par)continue;
dfs(nb, beg);
sub[beg][1] += sub[nb][1];
sub[beg][2] += sub[nb][2];
sum += dp[nb];
}
dp[beg] = max(dp[beg], sum);
dp[beg] = max(dp[beg], sub[beg][1] - sub[beg][2]);
}
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 + max(max(sub[x][2], sub[x][1]), dp[x] + sub[x][2]);
}
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
*/
# | 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... |