This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define mp make_pair
#define pb push_back
using namespace std;
int n, q;
vi G[100010], sum, size;
ll rez = 0;
void dfs(int at, int parent){
vl deca;
ll gol = G[at].size() - 1;
if(at == 0)
gol++;
for(auto it : G[at]){
if(it == parent)
continue;
dfs(it, at);
deca.pb(size[it]);
}
sort(deca.begin(), deca.end());
for(auto it : deca){
if(size[at] + it <= q){
size[at] += it;
gol--;
}
else{
rez += gol;
return;
}
}
}
int main(){
cin>>n>>q;
sum.resize(n);
size.resize(n);
ll mby = 0;
for(int i = 0; i < n; i++){
cin>>sum[i];
mby += sum[i];
size[i] = sum[i];
}
for(int i = 0; i < n - 1; i++){
int a, b;
cin>>a>>b;
a--; b--;
G[a].pb(b);
G[b].pb(a);
}
if(mby <= q)
{
cout<<0<<endl;
return 0;
}
dfs(0, -1);
cout<<rez<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |