# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1117788 | vjudge1 | Paprike (COI18_paprike) | C++17 | 56 ms | 8264 KiB |
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> sira(sz);
int n,k;
struct DSU{
vector<int> p,comp;
void init(){
p.resize(n+1);
comp.resize(n+1);
for(int i=1;i<=n;++i){
p[i] = i;
comp[i] = sira[i];
}
};
int FIND(int a){
return (p[a] == a ? a : p[a] = FIND(p[a]));
}
void UNI(int a,int b){
a = FIND(a);
b = FIND(b);
if(a == b)return;
comp[a] += comp[b];
p[b] = a;
}
};
signed main(){
cin >> n >> k;
for(int i=1;i<=n;++i){
cin >> sira[i];
}
int a,b;
vector<pair<int,int>> hmm(n);
for(int i=0;i<n-1;++i){
cin >> a >> b;
hmm[i] = {a,b};
}
if(n> 15){
DSU temp;
for(int i=0;i<hmm.size();++i){
a= temp.FIND(hmm[i].first);
b = temp.FIND(hmm[i].second);
if(a == b)continue;
if(temp.comp[a] + temp.comp[b] > k)continue;
temp.UNI(a,b);
}
unordered_set<int> st;
for(int i=1;i<=n;++i)st.insert(temp.FIND(i));
cout << st.size() - 1;
return 0;
}
int mink = n;
for(int mask=0;mask<(1 << hmm.size());++mask){
DSU temp;
temp.init();
for(int j=0;j<hmm.size();++j){
if((1 << j) & mask){
temp.UNI(hmm[j].first,hmm[j].second);
}
}
unordered_set<int> st;
bool valid = 1;
for(int i=1;i<=n;++i){
if(temp.comp[temp.FIND(i)] > k){
valid = 0;
break;
}
st.insert(temp.FIND(i));
}
if(!valid)continue;
a = st.size() - 1;
mink = min(mink,a);
}
cout << mink;
}
Compilation message (stderr)
# | 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... |