Submission #730912

#TimeUsernameProblemLanguageResultExecution timeMemory
730912MilosMilutinovicMagic Tree (CEOI19_magictree)C++14
0 / 100
2112 ms662980 KiB
/** * author: wxhtzdy * created: 26.04.2023 14:44:59 **/ #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, k; cin >> n >> m >> k; vector<vector<int>> g(n); for (int i = 1; i < n; i++) { int p; cin >> p; --p; g[p].push_back(i); g[i].push_back(p); } vector<int> a(n, -1), b(n); for (int i = 0; i < m; i++) { int v, d, w; cin >> v >> d >> w; --v; --d; a[v] = d; b[v] = w; } vector<int> rt(n); vector<vector<pair<int, int>>> vals(n); function<void(int, int)> Dfs = [&](int v, int pv) { int f = -1; for (int u : g[v]) { if (u == pv) { continue; } Dfs(u, v); if (f == -1 || vals[rt[f]].size() < vals[rt[u]].size()) { f = u; } } rt[v] = v; for (int u : g[v]) { if (u == pv) { continue; } for (auto& p : vals[rt[u]]) { vals[rt[v]].push_back(p); } } sort(vals[rt[v]].begin(), vals[rt[v]].end()); if (a[v] != -1) { for (auto& p : vals[rt[v]]) { if (p.first > a[v]) { long long s = 0; for (auto& q : vals[rt[v]]) { if (q.first >= p.first && q.second > 0) { s += q.second; } } vals[rt[v]].emplace_back(p.first, -min(s, 1LL * b[v])); break; } } vals[rt[v]].emplace_back(a[v], b[v]); } }; Dfs(0, 0); sort(vals[rt[0]].begin(), vals[rt[0]].end()); long long ans = 0; long long sum = 0; for (auto& p : vals[rt[0]]) { sum += p.second; ans = max(ans, sum); } cout << ans << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...