Submission #833735

# Submission time Handle Problem Language Result Execution time Memory
833735 2023-08-22T08:22:26 Z FEDIKUS Magic Tree (CEOI19_magictree) C++17
3 / 100
188 ms 35204 KB
#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);
    }
    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 Runtime error 10 ms 14648 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 76 ms 18972 KB Output is correct
2 Correct 61 ms 18384 KB Output is correct
3 Correct 188 ms 35204 KB Output is correct
4 Correct 115 ms 19820 KB Output is correct
5 Correct 153 ms 21092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 14932 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 88 ms 23192 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 14648 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 23 ms 16436 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 14648 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 14648 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -