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 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 (stderr)
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 |
---|
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... |