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;
int n,k;
int ljutina[100009];
vector<int> veze[100009];
int bio[100009],uk[100009],stv[100009];
int rez,mak;
int dfs(int x){
uk[x] = ljutina[x];
for(int i = 0;i<veze[x].size();i++){
if(bio[veze[x][i]]==0){
bio[veze[x][i]] = 1;
uk[x] += dfs(veze[x][i]);
}
else{
veze[x].erase(veze[x].begin()+i);
i--;
}
}
return uk[x];
}
void rijesi(int x){
bio[x] = 1;
int zbroj = ljutina[x];
vector<int> stvari,ind;
map<int,int> pomoc;
for(int i = 0;i<veze[x].size();i++){
if(bio[veze[x][i]]==0){
// cout << veze[x][i] << ' ' << bio[veze[x][i]] << ' ' << x << ' ';
bio[veze[x][i]] = 1;
stvari.push_back(uk[veze[x][i]]);
pomoc[uk[veze[x][i]]] = veze[x][i];
// cout << stvari[i]<< ' ' << uk[veze[x][i]] << ' ' << pomoc[stvari[i]] << "\n";
}
}
sort(stvari.begin(),stvari.end());
for(int i = 0;i<stvari.size();i++){
ind.push_back(pomoc[stvari[i]]);
// cout << stvari[i]<< ' ' << pomoc[stvari[i]] << ' ' << ind[i]<< ' ' << x <<' ' <<pomoc[12] << "\n";
}
for(int i = 0;i<stvari.size();i++){
if(zbroj+stvari[i]>k){
if(zbroj+ljutina[ind[i]]<=k){
k-=zbroj;
mak += zbroj;
rijesi(ind[i]);
k+=zbroj;
mak -= zbroj;
zbroj += stv[ind[i]];
}
else{
// cout << x << ' ' << ind[i] << "\n";
rez++;
if(stvari[i]>k+mak){
k+=mak;
int zap = mak;
mak = 0;
rijesi(ind[i]);
mak = zap;
k-=mak;
}
}
}
else{
zbroj += stvari[i];
}
}
stv[x] = zbroj;
}
int main(){
cin >> n >> k;
for(int i = 0;i<n;i++){
int a;
cin >> a;
ljutina[i] = a;
}
for(int i = 0;i<n-1;i++){
int a,b;
cin >> a >> b;
veze[a-1].push_back(b-1);
veze[b-1].push_back(a-1);
}
bio[0] = 1;
dfs(0);
rez = 0;
memset(bio,0,sizeof(bio));
bio[0] = 1;
mak = 0;
rijesi(0);
// cout << "\n";
// for(int i = 0;i<n;i++){
// cout << uk[i] << ' ' << stv[i] << "\n";
// }
cout << rez;
return 0;
}
Compilation message (stderr)
paprike.cpp: In function 'int dfs(int)':
paprike.cpp:13:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<veze[x].size();i++){
~^~~~~~~~~~~~~~~
paprike.cpp: In function 'void rijesi(int)':
paprike.cpp:31:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<veze[x].size();i++){
~^~~~~~~~~~~~~~~
paprike.cpp:41:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<stvari.size();i++){
~^~~~~~~~~~~~~~
paprike.cpp:45:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<stvari.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... |