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 ll = long long;
using namespace std;
const int mxN = 100005;
vector<pair<int,int>> aj[mxN];
int cut[mxN];
ll a[mxN];
int flag = 0;
ll K;
int f;
int dfs(int u, int p = 0) {
int sm = a[u - 1];
for (auto [v, i] : aj[u]) {
if (v ^ p) {
int r = dfs(v, u);
if (!cut[i]) {
sm += r;
}
}
}
if (sm > K) {
f = 0;
}
return sm;
}
int main() {
int N;
cin >> N >> K;
for (int i = 0; i < N; i ++) {
cin >> a[i];
}
vector<pair<int,int>> eg;
for (int i = 0; i < N - 1; i ++) {
int X, Y;
cin >> X >> Y;
eg.emplace_back(X, Y);
aj[X].emplace_back(Y, i);
aj[Y].emplace_back(X, i);
}
if (N <= 15) {
int mn = INT_MAX;
for (int i = 0; i < (1 << (N - 1)); i ++) {
for (int j = 0; j < N - 1; j ++) {
int bt = (i >> j) & 1;
if (bt) {
cut[j] = 1;
}
}
f = 1;
dfs(1);
if (f) {
mn = min(mn, __builtin_popcount(i));
}
for (int j = 0; j < N - 1; j ++) {
cut[j] = 0;
}
}
cout << mn << endl;
} else {
vector<int> b(N + 1);
for (int i = 1; i <= N; i ++) {
b[i] = a[i - 1];
}
for (int i = 1; i <= N; i ++) {
a[i] = b[i];
}
int cnt = 0;
int j = 0;
for (int i = 0; i < N;) {
ll sm = 0;
for (; i < N && sm + a[i + 1] <= K; i ++) {
sm += a[i + 1];
}
if (i != N) {
cnt ++;
}
}
cout << cnt << endl;
}
}
Compilation message (stderr)
paprike.cpp: In function 'int main()':
paprike.cpp:74:13: warning: unused variable 'j' [-Wunused-variable]
74 | int j = 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... |