#include <bits/stdc++.h>
using namespace std;
#define int long long
const int sz = 1e5+5;
int n, k;
vector<vector<array<int,2>>> g(sz);
vector<vector<int>> depthv(sz);
int w[sz], depth[sz];
bool used[sz];
signed main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin >> n >> k;
for(int i = 1;i <= n;i++) cin >> w[i];
for(int i = 1;i < n;i++) {
int a, b; cin >> a >> b;
g[a].push_back({w[b], b});
g[b].push_back({w[a], a});
}
queue<array<int,2>> q;
q.push({1, 0});
used[1] = 1;
int bt = 0;
while(!q.empty()) {
int u = q.front()[0];
int d = q.front()[1];
q.pop();
bt = max(bt, d);
depth[u] = d;
depthv[d].push_back(u);
for(auto &[we, to] : g[u]) {
if(used[to]) continue;
used[to] = 1;
q.push({to, d+1});
}
}
bt--;
int comp_cnt = n;
for(;bt >= 0;bt--) {
for(auto &u : depthv[bt]) {
for(auto &[we, to] : g[u]) we = w[to];
sort(g[u].begin(), g[u].end());
for(auto &[we, to] : g[u]) {
if(depth[to] != bt+1) continue;
if(w[u] + we <= k) {
w[u] += we;
comp_cnt--;
}
}
}
}
cout << comp_cnt - 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... |