이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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]);
}
}
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;
}
컴파일 시 표준 에러 (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:27:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<veze[x].size();i++){
~^~~~~~~~~~~~~~~
paprike.cpp:37:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<stvari.size();i++){
~^~~~~~~~~~~~~~
paprike.cpp:41: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... |