Submission #734710

#TimeUsernameProblemLanguageResultExecution timeMemory
734710bufferingPaprike (COI18_paprike)C++17
100 / 100
794 ms23388 KiB
#include <bits/stdc++.h>
using namespace std;

int MAXN = 100001;
vector<int> parent(MAXN);
vector<vector<int>> children(MAXN);
vector<pair<int, int>> layer(MAXN);
vector<vector<int>> adj(MAXN);
vector<pair<int, int>> dp(MAXN, {0, 0}); //{max size with node, min cuts}
 
void IO(string s = "")
{
    if (s == "")
    {
        freopen("input.txt", "r", stdin);
        freopen("output 2.txt", "w", stdout);
    }
    if (s != "")
    {
        freopen((s + ".in").c_str(), "r", stdin);
        freopen((s + ".out").c_str(), "w", stdout);
    }
}

void dfs(int node, int p, int l) {
    layer[node] = {l, node};
    for (int c: adj[node]) {
        if (c != p) {
            parent[c] = node;
            children[node].push_back(c);
            dfs(c, node, l + 1);
        }
    }
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    //IO();

    int n; int k;
    cin >> n >> k;
    
    vector<int> h(n);
    for (int i = 0; i < n; i++) {
        cin >> h[i];
    }

    for (int i = 0; i < n - 1; i++) {
        int u; int v;
        cin >> u >> v;
        u--; v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    dfs(0, 0, 0);

    sort(layer.begin(), layer.end());
    reverse(layer.begin(), layer.end());


    for (int i = 0; i < layer.size(); i++) {
        int node = layer[i].second;
        if (layer[node].first == 0) {
            dp[node] = {h[node], 0};
        }
        else {
            vector<int> s;
            int totcuts = 0;
            for (auto c: children[node]) {
                s.push_back(dp[c].first);
                totcuts += dp[c].second;
            }
            sort(s.begin(), s.end());
            int cuts = children[node].size();
            int curr = h[node];
            //cout << node << " " << " " << h[node] << " " << cuts << endl;
            for (int i = 0; i < s.size(); i++) {
                if (curr + s[i] > k) break;
                else {
                    cuts--;
                    curr += s[i];
                }
            }
            totcuts += cuts;
            //cout << node << " " << " " << h[node] << " " << totcuts << endl;
            dp[node] = {curr, totcuts};
        }
    }
    //cout << dp[6].second << endl;

    cout << dp[0].second << endl;

    

    
}

Compilation message (stderr)

paprike.cpp: In function 'int main()':
paprike.cpp:65:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |     for (int i = 0; i < layer.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
paprike.cpp:81:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |             for (int i = 0; i < s.size(); i++) {
      |                             ~~^~~~~~~~~~
paprike.cpp: In function 'void IO(std::string)':
paprike.cpp:15:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |         freopen("input.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
paprike.cpp:16:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |         freopen("output 2.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
paprike.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |         freopen((s + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
paprike.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |         freopen((s + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...