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 pb push_back
#define mp make_pair
const int siz = 1e5+1;
const int mod = 0;
struct node {
long long u, v;
};
vector<node> el;
vector<long long> par;
vector<long long> rnk;
vector<long long> value;
long long cuts = 0;
long long n, k;
long long get(long long a) {
return par[a] = (par[a] == a) ? a : get(par[a]);
}
void join(long long a, long long b) {
a = get(a); b = get(b);
if(a != b) {
if(rnk[a] == rnk[b]) rnk[a]++;
if(rnk[b] > rnk[a]) swap(a,b);
par[b] = a; value[a] += value[b];
}
}
bool good(long long a, long long b) {
a = get(a); b = get(b);
return value[a] + value[b] <= k;
}
void mincuts() {
for(int i=0; i<n-1; i++) {
long long x = el[i].u; long long y = el[i].v;
if(good(x,y)) {
join(x,y);
}
}
set<long long> s;
for(int i=1; i<=n; i++) {
long long x = get(i);
s.insert(x);
}
cuts = s.size()-1;
}
bool cmp(node a, node b) {
long long x = value[a.u] + value[a.v];
long long y = value[b.u] + value[b.v];
return x < y;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
value.resize(n+1); par.resize(n+1); rnk.resize(n+1);
for(int i=1; i<=n; i++) {
int x;
cin >> x;
value[i] = x;
}
for(int i=1; i<=n; i++) {
par[i] = i;
}
long long m = n-1;
for(int i=1; i<=m; i++) {
long long a, b;
cin >> a >> b;
el.pb({a,b});
}
sort(el.begin(), el.end(), cmp);
mincuts();
cout << cuts << "\n";
return 0;
}
# | 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... |