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>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize("Ofast")
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
/* #define endl '\n' */
#define indexed_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
const int MOD = 1e9+7;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, -1, 0, 1};
int n, k, a, b;
int ans = 0;
int h[100000] = {0};
vi graph[100000];
int dfs(int v, int p) {
vi spy;
/* cout << v << ": "; */
/* for (int c:graph[v]) cout << c << " "; */
/* cout << endl; */
for (int c:graph[v]) if (c != p) spy.push_back(dfs(c, v));
sort(all(spy));
int res = h[v];
for (int i=0; i<spy.size(); i++) {
if (res + spy[i] > k) {
ans += spy.size() - i;
return res;
}
res += spy[i];
}
return res;
}
void _main() {
cin >> n >> k;
for (int i=0; i<n; i++) {
cin >> h[i];
}
for (int i=0; i<n-1; i++) {
cin >> a >> b;
--a; --b;
graph[a].push_back(b);
graph[b].push_back(a);
}
dfs(0, -1);
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
_main();
return 0;
}
Compilation message (stderr)
paprike.cpp: In function 'int dfs(int, int)':
paprike.cpp:35:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for (int i=0; i<spy.size(); i++) {
| ~^~~~~~~~~~~
# | 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... |