#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});
while(it!=dp.end() && it->second<=f.second){
dp.erase(it);
it=dp.lower_bound({f.first,-1});
}
if(it==dp.end()){
dp.emplace(f);
return;
}
pii sled=*it;
dp.erase(it);
dp.emplace(nov);
sled.second-=f.second;
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 |
1 |
Incorrect |
4 ms |
7252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
19060 KB |
Output is correct |
2 |
Correct |
63 ms |
18292 KB |
Output is correct |
3 |
Correct |
188 ms |
35264 KB |
Output is correct |
4 |
Correct |
127 ms |
19748 KB |
Output is correct |
5 |
Correct |
220 ms |
21080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
7440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
115 ms |
15124 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
7252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
8344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
7252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
7252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |