Submission #473721

#TimeUsernameProblemLanguageResultExecution timeMemory
473721CSQ31Magic Tree (CEOI19_magictree)C++17
100 / 100
163 ms35392 KiB
#include<bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define se second #define mp make_pair #define all(a) a.begin(),a.end() #define sz(a) (int)(a.size()) #define lb lower_bound #define ub upper_bound #define owo ios_base::sync_with_stdio(0);cin.tie(0); #define MOD (ll)(1e9+7) #define INF (ll)(1e18) #define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr) #define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\ debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC)) typedef long long int ll; typedef long double ld; typedef pair<ll,ll> PII; typedef pair<int,int> pii; typedef vector<vector<int>> vii; typedef vector<vector<ll>> VII; const int MAXN = 1e5; vii adj(MAXN); ll d[MAXN],w[MAXN]; map<ll,ll>dp[MAXN]; void dfs(int v){ if(!sz(adj[v])){ dp[v][d[v]]+=w[v]; return; } for(int x:adj[v]){ dfs(x); if(sz(dp[x]) > sz(dp[v]))swap(dp[x],dp[v]); for(auto z:dp[x])dp[v][z.fi]+=z.se; } if(w[v]){ dp[v][d[v]]+=w[v]; ll cur = w[v]; while(true){ auto it = dp[v].ub(d[v]); if(it == dp[v].end())break; if(it->se <= cur){ cur-=it->se; dp[v].erase(it); }else{ it->se -= cur; break; } } } } int main() { owo int n,m,k; cin>>n>>m>>k; for(int i=1;i<n;i++){ int p; cin>>p; adj[p-1].pb(i); } while(m--){ int a,b,c; cin>>a>>b>>c; a--; d[a] = b; w[a] = c; } dfs(0); ll ans = 0; for(auto x:dp[0])ans+=x.se; cout<<ans; }
#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...