This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* author: wxhtzdy
* created: 30.06.2022 09:53:47
**/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
--u; --v;
g[u].push_back(v);
g[v].push_back(u);
}
int ans = 0;
vector<long long> f(n);
function<void(int, int)> Dfs = [&](int v, int pr) {
vector<int> ch;
for (int u : g[v]) {
if (u == pr) {
continue;
}
Dfs(u, v);
ch.push_back(u);
}
sort(ch.begin(), ch.end(), [&](int i, int j) {
return f[i] < f[j];
});
f[v] = a[v];
for (int i = 0; i < (int) ch.size(); i++) {
if (f[v] + f[ch[i]] <= k) {
f[v] += f[ch[i]];
} else {
ans += 1;
}
}
};
Dfs(0, 0);
cout << ans << '\n';
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... |