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;
int n, k;
vector <vector <int>> adj;
vector <int> vals;
int ans;
int dfs(int s, int p) {
int x = vals[s];
vector <int> tp;
for (auto i : adj[s]) {
if (i == p)
continue;
tp.push_back(dfs(i, s));
}
sort(tp.begin(), tp.end());
for (int i = 0; i < (int)tp.size(); i++) {
if (tp[i] + x <= k)
x += tp[i];
else
ans++;
}
return x;
}
int main() {
cin >> n >> k;
adj.resize(n+1);
vals.resize(n+1);
for (int i = 1; i <= n; i++)
cin >> vals[i];
for (int i = 0; i < n-1; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
if ((int)adj[i].size() == 1) {
dfs(i, i);
break;
}
}
cout << ans << '\n';
}
# | 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... |