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;
typedef pair<int, int> ii;
typedef long long ll;
const int INF = 1e9;
const int MAXN = 1e5 + 5;
const ll LINF = 1e18;
const double pi = acos(-1);
const double EPS = 1e-9;
template <class T> using MinPQ = priority_queue<T, vector<T>, greater<T>>;
template <class T> using MaxPQ = priority_queue<T>;
void scan() { return; } template<typename T, typename... Args> void scan(T& a, Args&... args) { cin >> a; scan(args...); }
void print() { return; } template<typename T, typename... Args> void print(T a, Args... args) { cout << a << " "; print(args...); }
int n, k;
int a[MAXN];
vector<vector<int>> g;
ll curSize;
bitset<MAXN> vis;
void dfs(int u) {
vis[u] = 1;
curSize += a[u];
for (int v : g[u]) {
if (!vis[v]) {
if (a[v] + curSize <= k) {
dfs(v);
}
}
}
}
bool comp(int i, int j) {
if (g[i].size() != g[j].size()) return g[i].size() < g[j].size();
if (a[i] != a[j]) return a[i] < a[j];
return i < j;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
scan(n, k);
for (int i = 0; i < n; i++) scan(a[i]);
g.assign(n, vector<int>());
vector<int> v = {0};
for (int i = 1; i < n; i++) {
int x, y;
scan(x, y); --x; --y;
g[x].emplace_back(y);
g[y].emplace_back(x);
v.emplace_back(i);
}
sort(v.begin(), v.end(), comp);
int ans = 0;
for (auto x : v) {
if (!vis[x]) {
curSize = 0LL;
dfs(x);
ans++;
}
}
printf("%d\n", ans - 1);
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... |