Submission #703486

#TimeUsernameProblemLanguageResultExecution timeMemory
703486CyanmondMagic Tree (CEOI19_magictree)C++17
47 / 100
2080 ms787112 KiB
#include <bits/stdc++.h> using i64 = long long; struct Vertice { bool exFruit; int day; i64 weight; }; int main() { int N, M, K; std::cin >> N >> M >> K; std::vector<int> P(N); std::vector<std::vector<int>> tree(N); for (int i = 1; i < N; ++i) { std::cin >> P[i]; --P[i]; tree[P[i]].push_back(i); } std::vector<int> U(M), D(M); std::vector<i64> W(M); std::vector<Vertice> vData(N); for (int i = 0; i < M; ++i) { std::cin >> U[i] >> D[i] >> W[i]; --U[i]; } { // press auto cD = D; std::sort(cD.begin(), cD.end()); cD.erase(std::unique(cD.begin(), cD.end()), cD.end()); for (auto &e : D) { e = (int)(std::lower_bound(cD.begin(), cD.end(), e) - cD.begin()); } K = (int)cD.size(); } for (int i = 0; i < M; ++i) { vData[U[i]] = {true, D[i], W[i]}; } auto dfs = [&](auto &&self, const int v) -> std::vector<i64> { // calc DP std::vector<std::vector<i64>> cDPs; for (const int t : tree[v]) { cDPs.push_back(self(self, t)); } // cut if (cDPs.size() == 1 and not vData[v].exFruit) { return cDPs[0]; } std::vector<i64> ret(K + 1); for (const auto &vec : cDPs) { assert((int)vec.size() == K + 1); for (int i = 0; i <= K; ++i) { ret[i] += vec[i]; } } if (vData[v].exFruit) { ret[vData[v].day] += vData[v].weight; for (int i = vData[v].day + 1; i <= K; ++i) { ret[i] = std::max(ret[i], ret[i - 1]); } } return ret; }; const auto res = dfs(dfs, 0); std::cout << res[K] << std::endl; }
#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...