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;
#define int long long
const int sz = 3e5 + 5;
vector<int> par(sz),comp(sz);
int FIND(int a){
return (par[a] == a ? a : par[a] = FIND(par[a]));
}
void UNI(int a,int b){
comp[b] += comp[a];
par[a] = b;
}
signed main(){
int n,k;
cin >> n >> k;
vector<int> sira(n+1);
for(int i=1;i<=n;++i){
cin >> sira[i];
comp[i] = sira[i];
par[i] = i;
}
int a,b;
for(int i=1;i<n;++i){
cin >> a >> b;
a = FIND(a);
b = FIND(b);
if(a == b)continue;
if(comp[a] + comp[b] > k)continue;
UNI(a,b);
}
unordered_set<int> st;
for(int i=1;i<=n;++i)st.insert(FIND(i));
cout << st.size() - 1;
}
# | 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... |