#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const int MAXN = 2e5 + 10;
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][2], 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 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);
f.push_back(make_pair(sub[x][1], x));
s.push_back(make_pair(sub[x][2], x));
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
*/
Compilation message
magictree.cpp: In function 'void dfs(int, int)':
magictree.cpp:51:15: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
51 | sub[beg][2] = 0;
| ~~~~~~~~~~^
magictree.cpp:53:22: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
53 | else sub[beg][2] += f[beg].second;
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~
magictree.cpp:65:52: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
65 | dp[beg] = max(dp[beg], sub[beg][1] - sub[beg][2]);
| ~~~~~~~~~~^
magictree.cpp:60:21: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
60 | sub[beg][2] += sub[nb][2];
| ~~~~~~~~~~~~^~~~~~~~~~~~~
magictree.cpp:60:33: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
60 | sub[beg][2] += sub[nb][2];
| ~~~~~~~~~^
magictree.cpp:60:21: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
60 | sub[beg][2] += sub[nb][2];
| ~~~~~~~~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
14672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
16464 KB |
Output is correct |
2 |
Correct |
22 ms |
17744 KB |
Output is correct |
3 |
Correct |
25 ms |
15696 KB |
Output is correct |
4 |
Correct |
20 ms |
15568 KB |
Output is correct |
5 |
Correct |
24 ms |
16068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
14672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
16720 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
14672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
15196 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
14672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
14672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |