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;
#define ll long long
#define ld long double
#define endl '\n'
#define pb push_back
#define speedyboy ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(x) x.begin() , x.end()
#define F first
#define S second
#define pll pair<ll , ll>
#define pss pair<string , string>
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
const ll sz = 5e5 + 5 , INF = 1e18 , MOD = 998244353;
ll a[sz];
vector<ll> g[sz];
ll ans;
void dfs(ll n , ll u , ll m , ll &cursum){
cursum = a[n];
vector<ll> v;
for(ll i : g[n]){
if(i != u){
ll changesum = 0;
dfs(i , n , m , changesum);
v.pb(changesum);
}
}
sort(all(v));
for(ll i = 0 ; i < v.size() ; i++){
if(cursum + v[i] > m){
ans++;
}
else{
cursum += v[i];
}
}
if(cursum > m){
ans++;
}
}
ll i , j , k;
void solve(){
ll n , m;
cin >> n >> m;
for(i = 1 ; i <= n ; i++){
cin >> a[i];
}
for(i = 1 ; i < n ; i++){
ll u , v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
ll cursum = 0;
dfs(1 , -1 , m , cursum);
cout << ans << endl;
}
signed main(){
speedyboy;
ll t = 1;
//cin >> t;
while(t--){
solve();
}
}
Compilation message (stderr)
paprike.cpp: In function 'void dfs(long long int, long long int, long long int, long long int&)':
paprike.cpp:35:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(ll i = 0 ; i < v.size() ; i++){
| ~~^~~~~~~~~~
# | 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... |