Submission #785870

#TimeUsernameProblemLanguageResultExecution timeMemory
785870AndiRPaprike (COI18_paprike)C++14
100 / 100
102 ms16080 KiB
#include <iostream>
#include <vector>
#include <queue>

using namespace std;
const int Nmax=100000;

int n, k, h[Nmax], sol;
vector <int> ad[Nmax];
priority_queue <int, vector<int>, greater<int>> pq;
void dfs(int node, int p){
    for (int i=0; i<ad[node].size(); i++){
        if (ad[node][i]!=p)
            dfs(ad[node][i], node);
    }
    for (int i=0; i<ad[node].size(); i++){
        if (ad[node][i]!=p)
            pq.push(h[ad[node][i]]);
    }
    int newh=h[node];
    while (!pq.empty()){
        if (newh+pq.top()<=k)
            newh+=pq.top();
        else sol++;
        pq.pop();
    }
    h[node]=newh;
}
int main()
{
    cin>>n>>k;
    for (int i=0; i<n; i++)
        cin>>h[i];
    int a, b;
    for (int i=0; i<n-1; i++){
        cin>>a>>b;
        a--; b--;
        ad[a].push_back(b);
        ad[b].push_back(a);
    }
    dfs(0, -1);
    cout<<sol;
    return 0;
}

Compilation message (stderr)

paprike.cpp: In function 'void dfs(int, int)':
paprike.cpp:12:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 |     for (int i=0; i<ad[node].size(); i++){
      |                   ~^~~~~~~~~~~~~~~~
paprike.cpp:16:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |     for (int i=0; i<ad[node].size(); i++){
      |                   ~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...