#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];
int v[MAXN], d[MAXN], w[MAXN];
vector < int > g[MAXN];
pair < int, int > f[MAXN];
void read()
{
cin >> n >> m >> k;
for (int i = 2; i <= n; ++ i)
{
cin >> p[i];
g[p[i]].push_back(i);
}
for (int i = 1; i <= m; ++ i)
{
cin >> v[i] >> d[i] >> 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][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;
for (auto nb: g[beg])
{
if(nb == par)continue;
dfs(nb, beg);
sub[beg][1] += sub[nb][1];
sub[beg][2] += sub[nb][2];
dp[beg] = max(dp[beg], dp[nb]);
}
dp[beg] = max(dp[beg], sub[beg][1] - sub[beg][2]);
}
int main()
{
speed();
read();
dfs(1, 0);
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 = max(ans, dp[x] + sub[x][2]);
}
sort(f.begin(), f.end(), cmp);
sort(s.begin(), s.end(), cmp);
long long case1 = f[0].first;
if(s[0].second == f[0].second && s.size() > 1)case1 += s[1].first;
long long case2 = s[0].second;
if(f[0].second == s[0].second && f.size() > 1)case2 += f[1].first;
ans = max(ans, case1);
ans = max(ans, case2);
cout << ans << endl;
return 0;
}
Compilation message
magictree.cpp: In function 'void dfs(int, int)':
magictree.cpp:38:15: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
38 | sub[beg][2] = 0;
| ~~~~~~~~~~^
magictree.cpp:40:22: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
40 | else sub[beg][2] += f[beg].second;
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~
magictree.cpp:49:52: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
49 | dp[beg] = max(dp[beg], sub[beg][1] - sub[beg][2]);
| ~~~~~~~~~~^
magictree.cpp:46:21: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
46 | sub[beg][2] += sub[nb][2];
| ~~~~~~~~~~~~^~~~~~~~~~~~~
magictree.cpp:46:33: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
46 | sub[beg][2] += sub[nb][2];
| ~~~~~~~~~^
magictree.cpp:46:21: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
46 | sub[beg][2] += sub[nb][2];
| ~~~~~~~~~~~~^~~~~~~~~~~~~
magictree.cpp: In function 'int main()':
magictree.cpp:64:39: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
64 | s.push_back(make_pair(sub[x][2], x));
| ~~~~~~~~^
magictree.cpp:65:40: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
65 | ans = max(ans, dp[x] + sub[x][2]);
| ~~~~~~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
4944 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
10312 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
5200 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
43 ms |
11344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
4944 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
6224 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
4944 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
4944 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |