이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define in insert
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
const ll INF = 1e18;
const int inf = INT_MAX;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
const int N = 1e5 + 5;
const int A = 26;
ll n, k, ans, a[N], d[N];
vector < int > g[N];
void dfs(int v, int p){
vector < int > val;
d[v] = a[v];
for(int i = 0; i < sz(g[v]); i++){
int to = g[v][i];
if(to != p){
dfs(to, v);
d[v] += d[to];
val.pb(d[to]);
}
}
sort(all(val));
reverse(all(val));
for(int i = 0; i < sz(val); i++){
if(d[v] > k){
d[v] -= val[i];
ans++;
}
else break;
}
}
int main(){
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
for(int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dfs(1, 0);
cout << ans;
}
# | 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... |