Submission #927836

#TimeUsernameProblemLanguageResultExecution timeMemory
92783612345678Magic Tree (CEOI19_magictree)C++17
100 / 100
93 ms33588 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long const int nx=1e5+5; ll n, m, k, t[nx], w[nx], p, l, res; vector<int> d[nx]; map<ll, ll> dp[nx]; void dfs(int u) { for (auto v:d[u]) { dfs(v); if (dp[v].size()>dp[u].size()) swap(dp[u], dp[v]); for (auto [x, y]:dp[v]) dp[u][x]+=y; dp[v].clear(); } int vl=w[u]; dp[u][t[u]]+=w[u]; for (auto itr=next(dp[u].find(t[u])); itr!=dp[u].end();) { if (vl>=itr->second) { vl-=itr->second; itr=dp[u].erase(itr); } else { itr->second-=vl; break; } } } int main() { cin.tie(NULL)->sync_with_stdio(false); cin>>n>>m>>k; for (int i=2; i<=n; i++) cin>>p, d[p].push_back(i); for (int i=1; i<=m; i++) cin>>l>>t[l]>>w[l]; dfs(1); for (auto [x, y]:dp[1]) res+=y; cout<<res<<'\n'; }
#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...