This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll,ll>;
const ll maxn=1e5+10;
ll p[maxn];
vector<ll> g[maxn];
pair<ll,ll> f[maxn];
set<pair<ll,ll> > dp[maxn]; // obrisao sam ceo subtree pre first-og dana i zaradio second vise nego za prosli koji imam sacuvan
void dodajsub(pair<ll,ll> f,set<pii> &dp){
auto it=dp.lower_bound({f.first,-1});
if(it==dp.end()){
dp.emplace(f);
return;
}
if(it->first!=f.first){
dp.emplace(f);
return;
}
pii nov=*it;
nov.second+=f.second;
dp.erase(it);
dp.emplace(nov);
}
void dodajcale(pii f,set<pii> &dp){
auto it=dp.lower_bound({f.first,-1});
if(it==dp.end()){
dp.emplace(f);
return;
}
pii nov=f;
if(it->first==f.first){
nov.second+=it->second;
dp.erase(it);
}
it=dp.lower_bound({f.first,-1});
ll tren=0;
while(it!=dp.end() && tren+it->second<=f.second){
tren+=it->second;
dp.erase(it);
it=dp.lower_bound({f.first,-1});
}
if(it==dp.end()){
dp.emplace(nov);
return;
}
pii sled=*it;
dp.erase(it);
dp.emplace(nov);
sled.second-=f.second-tren;
dp.emplace(sled);
}
void dfs(ll node=1){
for(ll i:g[node]) dfs(i);
pair<ll,ll> maxi={-1,-1};
for(ll i:g[node]) maxi=max(maxi,pii(dp[i].size(),i));
if(maxi.second!=-1){
swap(dp[node],dp[maxi.second]);
for(ll i:g[node]){
if(i==maxi.second) continue;
for(auto j:dp[i]){
dodajsub(j,dp[node]);
}
}
}
if(f[node].first!=-1) dodajcale(f[node],dp[node]);
/*cout<<node<<":\n";
for(auto i:dp[node]) cout<<i.first<<" "<<i.second<<"\n";
cout<<"\n";*/
}
int main(){
ll n,m,k;
cin>>n>>m>>k;
for(ll i=2;i<=n;i++){
cin>>p[i];
g[p[i]].push_back(i);
}
for(ll i=1;i<=n;i++) f[i]={-1,-1};
for(ll i=0;i<m;i++){
ll v,d,w; cin>>v>>d>>w;
f[v]={d,w};
}
dfs();
ll res=0;
for(auto i:dp[1]) res+=i.second;
cout<<res;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |