Submission #579378

#TimeUsernameProblemLanguageResultExecution timeMemory
579378tengiz05Magic Tree (CEOI19_magictree)C++17
19 / 100
2076 ms16644 KiB
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m, k; cin >> n >> m >> k; vector<int> p(n, -1); vector<vector<int>> e(n); for (int i = 1; i < n; i++) { cin >> p[i]; p[i]--; e[p[i]].push_back(i); } vector<tuple<int, int, int>> a(m); for (auto &[d, v, w] : a) { cin >> v >> d >> w; v--; } vector<int> dep(n); function<void(int)> dfs = [&](int u) { for (int v : e[u]) { dep[v] = dep[u] + 1; dfs(v); } }; dfs(0); sort(a.begin(), a.end(), [&](const tuple<int, int, int> &a, const tuple<int, int, int> &b) { if (get<0>(a) != get<0>(b)) return get<0>(a) < get<0>(b); return dep[get<1>(a)] > dep[get<1>(b)]; }); vector<i64> dp(n); for (auto [d, v, w] : a) { int u = v; i64 x = dp[u]; dp[u] = w; for (int v : e[u]) { dp[u] += dp[v]; } dp[u] = max(dp[u], x); while (u != -1) { i64 x = dp[u]; dp[u] = 0; for (int v : e[u]) { dp[u] += dp[v]; } dp[u] = max(dp[u], x); u = p[u]; } } cout << dp[0] << "\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...