Submission #990466

#TimeUsernameProblemLanguageResultExecution timeMemory
990466asdasdqwerMagic Tree (CEOI19_magictree)C++14
100 / 100
138 ms37972 KiB
#include <bits/stdc++.h>
using namespace std;

#define int int64_t

signed main() {
    int n,m,k;cin>>n>>m>>k;
    vector<int> p(n, -1);
    vector<vector<int>> child(n);

    for (int i=1;i<n;i++) {
        int c;cin>>c;c--;
        p[i]=c;
        child[c].push_back(i);
    }

    vector<int> day(n), juice(n);

    for (int i=0;i<m;i++) {
        int v,d,w;cin>>v>>d>>w;v--;d--;
        day[v]=d;
        juice[v]=w;
    }

    vector<map<int,int>> dp(n);
    function<void(int)> dfs=[&](int node) {
        if (child[node].size() == 0) {
            dp[node][day[node]] = juice[node];
            return;
        }

        int mx = 0;
        for (int x : child[node]) {
            dfs(x);
            mx = max(mx, (int)dp[x].size());
        }

        int id = -1;
        for (int x : child[node]) {
            if ((int)dp[x].size() == mx) {
                dp[x].swap(dp[node]);
                id = x;
                break;
            }
        }

        for (int x : child[node]) {
            if (x == id) continue;
            for (auto [time, inc] : dp[x]) {
                dp[node][time] += inc;
            }
        }

        vector<int> rem;
        int sm = 0;
        auto it = dp[node].upper_bound(day[node]);

        while (it != dp[node].end() && sm <= juice[node]) {
            sm += (*it).second;
            if (sm <= juice[node]) {
                rem.push_back((*it).first);
            }

            it = next(it);
        }

        for (int x:rem) {
            dp[node].erase(x);
        }

        dp[node][day[node]] += juice[node];
        
        it = dp[node].upper_bound(day[node]);
        if (it != dp[node].end()) {
            dp[node][(*it).first] = sm - juice[node];
        }
    };
    
    dfs(0);

    int sm = 0;
    for (auto [x, y] : dp[0]) {
        sm += y;
    }

    cout<<sm<<"\n";
}

Compilation message (stderr)

magictree.cpp: In lambda function:
magictree.cpp:49:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   49 |             for (auto [time, inc] : dp[x]) {
      |                       ^
magictree.cpp: In function 'int main()':
magictree.cpp:82:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   82 |     for (auto [x, y] : dp[0]) {
      |               ^
#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...