Submission #915057

#TimeUsernameProblemLanguageResultExecution timeMemory
915057PM1Magic Tree (CEOI19_magictree)C++17
100 / 100
141 ms36948 KiB
#include <bits/stdc++.h> using namespace std; #define fr first #define sc second #define ll long long const int mxn=1e5+5; int n,m,k,par[mxn],comp[mxn]; pair<int,int>f[mxn]; set<pair<int,ll>>s[mxn]; vector<int>v[mxn]; void up(int z,int x){ ll w=f[z].sc,d=f[z].fr,res=0; auto y=s[x].lower_bound({d,(ll)-1e18}); if(y!=s[x].end() && y->fr==d){ s[x].erase(y); res=y->sc; } s[x].insert({d,res+w}); while(w>0){ y=s[x].upper_bound({d+1,(ll)-1e18}); if(y==s[x].end()) break; ll res=y->sc,dd=y->fr; s[x].erase(y); if(res>w) s[x].insert({dd,res-w}); w-=res; } } void dsu(int x,int y){ for(auto i:s[y]){ auto w=s[x].lower_bound({i.fr,(ll)-1e18}); ll res=0; if(w !=s[x].end() && w->fr==i.fr){ s[x].erase(w); res+=w->sc; } s[x].insert({i.fr,i.sc+res}); } } void dfs(int z){ comp[z]=z; for(auto i:v[z]){ if(i!=par[z]){ dfs(i); if(s[comp[i]].size()>s[comp[z]].size()) comp[z]=comp[i]; } } for(auto i:v[z]){ if(i!=par[z] && comp[z]!=comp[i]){ dsu(comp[z],comp[i]); } } up(z,comp[z]); } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>m>>k; for(int i=2;i<=n;i++){ cin>>par[i]; v[par[i]].push_back(i); } for(int i=1;i<=m;i++){ int x,y,w; cin>>x>>y>>w; f[x]={y,w}; } dfs(1); ll ans=0; for(auto i:s[comp[1]]) ans+=i.sc; cout<<ans<<'\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...