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 long double ld;
typedef unsigned int ui;
#define ii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vii vector<ii>
#define vll vector<ll>
#define vpll vector<pll>
#define matrix vector<vi>
#define matrixLL vector<vLL>
#define vs vector<string>
#define vui vector<ui>
#define msi multiset<int>
#define mss multiset<string>
#define si set<int>
#define ss set<string>
#define PB push_back
#define PF push_front
#define PPB pop_back
#define PPF pop_front
#define X first
#define Y second
#define MP make_pair
#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const int dxx[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dyy[] = {0, 0, -1, 1, -1, 1, -1, 1};
const string abc="abcdefghijklmnopqrstuvwxyz";
const string ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const ld pi = 3.14159265;
const int mod = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 7;
const int inf = mod;
const ll INF = 1e18;
const ll zero = ll(0);
const int logo = 20;
const int off = 1 << logo;
const int trsz = off << 1;
ll s[MAXN];
ll val[MAXN], n, k;
vi g[MAXN];
bool cmp(int a, int b){
return a > b;
}
int rek(int u, int p = -1){
int ret = 0;
vll tmp; tmp.clear();
s[u] = val[u];
for(auto x: g[u]) if(x != p){
ret += rek(x, u);
s[u] += s[x];
tmp.PB(s[x]);
}
if(s[u] <= k) return ret;
sort(all(tmp), cmp);
for(int i=0; i<tmp.size(); i++){
ret++, s[i] -= tmp[i];
if(s[i] <= k) return ret;
}
}
void solve(){
cin >> n >> k;
for(int i=0; i<n; i++) cin >> val[i];
for(int i=1, a, b; i<n; i++) cin >> a >> b, g[a].PB(b), g[b].PB(a);
cout << rek(1, -1) << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t=1;
//cin >> t;
while(t--)solve();
return 0;
}
Compilation message (stderr)
paprike.cpp: In function 'int rek(int, int)':
paprike.cpp:74:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for(int i=0; i<tmp.size(); i++){
| ~^~~~~~~~~~~
paprike.cpp:61:6: warning: control reaches end of non-void function [-Wreturn-type]
61 | vll tmp; tmp.clear();
| ^~~
# | 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... |