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;
int spicy[100005];
vector<int> G[100005];
int cuts[100005];
int dfs(int teme, int parent) {
vector<int> subtree_spice;
int vk_spice=spicy[teme];
for (auto next:G[teme]) {
if (next==parent) continue;
int klk_spice=dfs(next, teme);
subtree_spice.push_back(klk_spice);
vk_spice+=klk_spice;
cuts[teme]+=cuts[next];
}
if (vk_spice<=k) return vk_spice;
sort(subtree_spice.begin(), subtree_spice.end());
int zbir=spicy[teme];
for (auto x:subtree_spice) {
if (zbir+x<=k) zbir+=x;
else cuts[teme]++;
}
return zbir;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i=1;i<=n;i++)
cin >> spicy[i];
for (int i=0;i<n-1;i++) {
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
dfs(1, 0);
cout << cuts[1];
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... |