#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<int>> graph;
vector<int> h;
int n, k;
int cuts = 0;
// DFS возвращает сумму в поддереве, которую можно оставить соединенной с родителем
int dfs(int node, int parent) {
int total = h[node]; // Начинаем с остроты текущего перца
for (int neighbor : graph[node]) {
if (neighbor == parent) continue;
int child_sum = dfs(neighbor, node);
// Если добавление поддерева ребенка не превышает k - добавляем
if (total + child_sum <= k) {
total += child_sum;
} else {
// Иначе - делаем разрез
cuts++;
}
}
return total;
}
int main() {
cin >> n >> k;
h.resize(n);
graph.resize(n);
// Читаем остроты перцев
for (int i = 0; i < n; i++) {
cin >> h[i];
}
// Строим дерево
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--; v--; // Переводим в 0-индексацию
graph[u].push_back(v);
graph[v].push_back(u);
}
// Запускаем DFS из произвольной вершины (0)
dfs(0, -1);
cout << cuts << endl;
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... |