이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define in insert
#define fi first
#define se second
#define vl vector<ll>
#define all(v) v.begin(), v.end()
#define endl "\n"
using namespace std;
const int sz = 1e5 + 123; /// mind this
const int MAX = 2e6 + 123;
const int BS = 61;
const ll inf = 1e17;
const int mod = 998244353;
ll c[sz], s[sz], par[sz], ans, k;
vl e[sz];
void init(ll v, ll p){
par[v] = p;
for(auto to: e[v]){
if(to != p){
init(to, v);
s[v] += s[to];
}
}
s[v] += c[v];
}
ll dfs(ll v, ll p){
if(s[v] <= k){return s[v];}
vl children;
for(auto to: e[v]){
if(to != p){
s[v] -= s[to];
children.pb(dfs(to, v));
s[v] += s[to];
}
}
if(s[v] > k){
sort(all(children));
while(s[v] > k){
s[v] -= children.back();
ans ++;
}
}
assert(s[v] <= k);
return s[v];
}
void solve(){
ll n, i, j, u, v;
cin >> n >> k;
for(i = 1; i <= n; i++){
cin >> c[i];
}
for(i = 1; i < n; i++){
cin >> u >> v;
e[u].pb(v);
e[v].pb(u);
}
init(1, 0);
dfs(1, 0);
cout << ans << endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
// cin >> t;
while(t--){
solve();
}
}
/*
1
2
9 0
*/
컴파일 시 표준 에러 (stderr) 메시지
paprike.cpp: In function 'void solve()':
paprike.cpp:51:14: warning: unused variable 'j' [-Wunused-variable]
51 | ll n, i, j, u, v;
| ^
# | 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... |