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 - alimammadzade
#include <bits/stdc++.h>
#define int long long
using namespace std;
struct dsu {
vector<int> p;
dsu(int n) { p.resize(n + 1); }
int _find(int u) { return (p[u] < 0 ? u : p[u] = _find(p[u])); }
void _union(int u, int v) { p[u] += p[v]; p[v] = u; }
};
signed main() {
cin.tie(nullptr)->sync_with_stdio(0);
// system("cls"), freopen("in.txt", "r", stdin);
int n, k, res = 20;
cin >> n >> k;
vector<int> w(n + 1);
vector<array<int, 2>> e(n - 1);
for (int i = 1; i <= n; i++) cin >> w[i];
for (auto& [u, v] : e) cin >> u >> v;
for (int i = 0; i < (1 << (n - 1)); i++) {
dsu d(n);
for (int j = 1; j <= n; j++) d.p[j] = -w[j];
for (int j = 0; j < n - 1; j++) if (i & (1 << j)) {
int u = d._find(e[j][0]), v = d._find(e[j][1]);
if (u != v) d._union(u, v);
}
bool ok = 1;
for (int j = 1; j <= n; j++) ok &= (-d.p[d._find(j)] <= k);
if (ok) res = min(res, n - 1 - __builtin_popcount(i));
}
cout << res;
}
# | 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... |