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;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll LOGN = 20;
const ll MAXN = 1e5 + 100;
vector<vector<int>> graph;
ll h[MAXN];
int k, ans = 0;
int dfs(int node, int parent) {
vector<int> v;
for (auto u : graph[node]) {
if (u == parent)
continue;
v.push_back(dfs(u, node));
if (v.back() == 0)
v.pop_back();
}
int rem = k - h[node];
sort(v.begin(), v.end(), greater<int>());
while (v.size()) {
if (v.back() <= rem) {
rem -= v.back();
v.pop_back();
} else
break;
}
ans += v.size();
return (k - rem);
}
int main() {
fast
int n, a, b;
cin >> n >> k;
graph = vector<vector<int>>(n+1, vector<int>());
for (int i = 1; i <= n; i++)
cin >> h[i];
for (int i = 1; i < n; i++) {
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
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... |