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>
using namespace std;
int n, k;
struct dsu {
vector<int> p;
dsu(int n) { p.assign(n + 1, -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; }
};
void fiSubtask() {
vector<int> h(n + 1);
for (int i = 1; i <= n; i++) cin >> h[i];
vector<array<int, 2>> e;
for (int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
e.push_back({ u, v });
}
int res = n;
for (int i = 0; i < (1 << (n - 1)); i++) {
dsu d(n);
for (int j = 1; j <= n; j++) d.p[i] = -h[i];
for (int j = 0; j < n - 1; j++)
if ((i & (1 << j)) and d._find(e[j][0]) != d._find(e[j][1])) d._union(d._find(e[j][0]), d._find(e[j][1]));
map<int, int> mp;
for (int i = 1; i <= n; i++) mp[d._find(i)] = -d.p[d._find(i)];
bool ok = 1;
for (auto [a, b] : mp) if (b > k) { ok = 0; break; }
if (ok) res = min(res, n - __builtin_popcount(i) - 1);
}
cout << res;
}
void seSubtask() {
vector<int> h(n + 1);
for (int i = 1; i <= n; i++) cin >> h[i];
for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; }
int res = 0, cur = 0;
for (int i = 1; i <= n; i++) {
if (cur + h[i] > k) res++, cur = 0;
cur += h[i];
}
cout << res;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(0);
// system("cls"), freopen("in.txt", "r", stdin);
cin >> n >> k;
if (n <= 15) fiSubtask();
else seSubtask();
}
# | 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... |