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 <iostream>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
const int mx = 100'000;
int n;
ll k;
vi edge[1 + mx];
vll h(1 + mx);
vi cuts(1 + mx);
vll topct(1 + mx);
void dfs(int u, int p)
{
vll lst;
topct[u] = h[u];
for(int v : edge[u])
{
if(v == p) continue;
dfs(v, u);
cuts[u] += cuts[v];
topct[u] += topct[v];
lst.push_back(topct[v]);
}
sort(lst.begin(), lst.end());
while(topct[u] > k)
{
topct[u] -= lst.back();
cuts[u]++;
lst.pop_back();
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> h[i];
for(int e = 1; e <= n-1; e++)
{
int u, v;
cin >> u >> v;
edge[u].push_back(v);
edge[v].push_back(u);
}
dfs(1, 0);
cout << cuts[1] << '\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... |