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>
#define ll long long
#define TASKNAME ""
using namespace std;
const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;
int n, k;
vector <vector <int> > gr;
vector <int> w;
int res = 0;
int dfs(int v, int p) {
if (p != -1 && gr[v].size() == 1) {
return w[v];
}
int sum = w[v];
vector <int> all;
for (int u : gr[v]) {
if (u == p) continue;
all.push_back(dfs(u, v));
}
sort(all.begin(), all.end());
for (int i = 0; i < (int) all.size(); i++) {
if (sum + all[i] > k) {
res += (int) all.size() - i;
break;
}
sum += all[i];
}
//cout << res << " " << sum << "\n";
return sum;
}
int main() {
#ifdef MY
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#else
//freopen(TASKNAME".in", "r", stdin);
//freopen(TASKNAME".out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#endif // MY
cin >> n >> k;
gr.resize(n);
w.resize(n);
for (int i = 0; i < n; i++) {
cin >> w[i];
}
for (int i = 1; i < n; i++) {
int v, u;
cin >> v >> u;
v--, u--;
gr[v].push_back(u);
gr[u].push_back(v);
}
dfs(0, -1);
res++;
cout << res - 1;
}
# | 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... |