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;
vector<int> adjList[100001];
int arr[100001];
int sum[100001];
int ans = 0;
void DFS(int node, int parent) {
for (auto child: adjList[node]) {
if (child != parent) {
DFS(child, node);
}
}
if ((int)adjList[node].size() == 1 && node != 1) {
sum[node] = arr[node];
}
else {
int cnt = arr[node];
priority_queue<int> pq;
for (auto child: adjList[node]) {
if (child != parent) {
pq.push(-sum[child]);
}
}
int cuts = 0;
while (!pq.empty()) {
int w = -pq.top();
cnt += w;
pq.pop();
if (cnt > K) {
cnt -= w;
cuts = (int)pq.size()+1;
break;
}
}
while (!pq.empty()) pq.pop();
sum[node] = cnt;
ans += cuts;
}
}
int main() {
cin >> N >> K;
for (int i = 1; i <= N; i++) {
cin >> arr[i];
}
for (int i = 0; i < N-1; i++) {
int u, v;
cin >> u >> v;
adjList[u].push_back(v);
adjList[v].push_back(u);
}
DFS(1, -1);
cout << ans << "\n";
}
# | 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... |