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>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FOR(i,a) for (int i=0;i<(a);++i)
#define FORD(i,a) for (int i=(a)-1;i>=0;i--)
#define FORT(i,a,b) for (int i=(a);i<=(b);++i)
#define FORTD(i,b,a) for (int i=(b);i>=(a);--i)
#define trav(i,v) for (auto i : v)
#define all(v) v.begin(),v.end()
#define ad push_back
#define fr first
#define sc second
#define mpr(a,b) make_pair(a,b)
#define pir pair<int,int>
#define all(v) v.begin(),v.end()
#define make_unique(v) v.erase(unique(all(v)),v.end())
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define srng mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define y1 EsiHancagorcRepa
const int N=4e5+10;
const ll MOD2=998244353;
const ll MOD=1e9+7;
ll w[N],ans,k;
vector<int> g[N];
ll dfs(int v,int par=-1){
//cout<<v<<endl;
ll sum=0;
vector<int> vc;
trav(to,g[v])if(to!=par)vc.ad(dfs(to,v));
sum+=w[v];
sort(all(vc));
ll ver=-1;
trav(tv,vc){
//if (to==par)continue;
if (sum>k){
ans++;
continue;
}
sum+=tv;
if (sum>k){
ans++;
ver=sum-tv;
}
}
if (ver!=-1)return ver;
return sum;
}
int main(){
fastio;
int n;
cin>>n>>k;
FORT(i,1,n){
cin>>w[i];
}
FOR(i,n-1){
int a,b;
cin>>a>>b;
g[a].ad(b);
g[b].ad(a);
}
dfs(1);
cout<<ans<<endl;
return 0;
}
# | 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... |