#include <bits/stdc++.h>
#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())
using namespace std;
template<typename U, typename V> bool maxi(U &a, V b) {
if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool mini(U &a, V b) {
if (a > b) { a = b; return 1; } return 0;
}
const int N = (int)2e5 + 9;
const int mod = (int)1e9 + 7;
void prepare(); void main_code();
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
const bool MULTITEST = 0; prepare();
int num_test = 1; if (MULTITEST) cin >> num_test;
while (num_test--) { main_code(); }
}
void prepare() {};
int n, m, k;
int s[N], y[N], re[N];
pair<int, int> a[N];
vector<int> g[N], adj[N];
int cnt = 0;
void dfs(int u) {
s[u] = y[u];
vec<int> c;
for(int v : g[u]) {
dfs(v);
s[u] += s[v];
if (s[v] > 0) {
c.push_back(v);
}
}
if (!y[u] and sz(c) == 0) {
re[u] = -1;
return ;
}
if (!y[u]) {
if (sz(c) == 1) {
re[u] = re[c.back()];
return ;
}
re[u] = u;
for(int v : c)
adj[u].push_back(re[v]);
return ;
}
re[u] = u;
for(int v : c)
adj[u].push_back(re[v]);
}
int mx[N];
long long dp[N];
void dfx(int u, int d, long long &c) {
if (mx[u] < d) {
c += dp[u];
return ;
}
if (a[u].fi == d)
c += a[u].se;
for(int v : adj[u]) {
dfx(v, d, c);
}
}
void dfs2(int u) {
dp[u] = 0;
mx[u] = a[u].first;
for(int v : adj[u]) {
dfs2(v);
dp[u] += dp[v];
maxi(mx[u], mx[v]);
}
long long nw = 0;
dfx(u, a[u].first, nw);
maxi(dp[u], nw);
}
void main_code() {
cin >> n >> m >> k;
for(int i = 2; i <= n; ++i) {
int x; cin >> x;
g[x].push_back(i);
}
y[1] = 1; a[1] = _mp(k + 1, 0);
for(int i = 1; i <= m; ++i) {
int v, d, w; cin >> v >> d >> w;
y[v] = 1;
a[v] = _mp(d, w);
}
dfs(1);
dfs2(1);
cout << dp[1];
}
/* Let the river flows naturally */
Compilation message
magictree.cpp: In function 'int main()':
magictree.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
15696 KB |
Output is correct |
2 |
Correct |
1010 ms |
21076 KB |
Output is correct |
3 |
Correct |
41 ms |
16720 KB |
Output is correct |
4 |
Correct |
30 ms |
16004 KB |
Output is correct |
5 |
Execution timed out |
2064 ms |
17872 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
10072 KB |
Output is correct |
2 |
Incorrect |
4 ms |
10076 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
55 ms |
18004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
10900 KB |
Output is correct |
2 |
Correct |
22 ms |
15276 KB |
Output is correct |
3 |
Correct |
24 ms |
14940 KB |
Output is correct |
4 |
Correct |
28 ms |
15196 KB |
Output is correct |
5 |
Correct |
11 ms |
13528 KB |
Output is correct |
6 |
Incorrect |
24 ms |
17244 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |