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;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define END "\n"
#define rall(v) (v).rbegin(), (v).rend()
#define all(v) (v).begin(), (v).end()
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void solve(){
int n, k;
cin >> n >> k;
vector<ll> A(n);
for (int i = 0; i < n; ++i) cin >> A[i];
vector<vector<int>> adj(n);
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
--a, --b;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<ll> sum(n), dp(n);
auto dfs = [&] (auto&& self, int v, int p) -> void {
sum[v] += A[v];
for (auto u: adj[v]) {
if (u != p) {
self(self, u, v);
dp[v] += dp[u];
sum[v] += sum[u];
}
}
if (sum[v] <= k) return;
vector<int> idxes;
for (auto u: adj[v]) {
if (u != p) idxes.push_back(u);
}
sort(all(idxes), [&] (int a, int b) {
return sum[a] > sum[b];
});
for (auto u: idxes) {
sum[v] -= sum[u];
++dp[v];
if (sum[v] <= k) break;
}
};
dfs(dfs, 0, -1);
cout << dp[0] << END;
}
int main()
{
fastio
int t = 1;
// cin>> t;
while(t--) solve();
return 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... |