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;
const int MAXN = 2e5+5;
int n, m, q;
typedef long long ll;
typedef array<int, 2> ar2;
int uf[MAXN];
int val[MAXN];
ar2 edges[2*MAXN];
const int inf = 1e9;
int find(int i) {
return uf[i] == i ? i : uf[i] = find(uf[i]);
}
int wt(ar2 i) {
return val[i[0]] + val[i[1]];
}
struct cmp {
bool operator()(ar2 i, ar2 j) {
return ar2({wt(i), i[0]}) < ar2({wt(j), j[0]});
}
} cmp;
int merge(int a, int b) {
if (val[a] > val[b]) swap(a, b);
uf[b] = a;
return val[b];
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> m >> q;
// assert(q == 0);
iota(uf, uf+n, 0);
ll s = 0;
int mx = 0, mn = inf;
for (int i = 0; i < n; i++) {
cin >> val[i];
s -= val[i];
mx = max(val[i], mx);
mn = min(val[i], mn);
}
s += mx;
for (int i = 0; i < m; i++) {
int a, b; cin >> a >> b;
edges[i] = {a-1, b-1};
}
sort(edges, edges+m, cmp);
vector<int> savings;
for (int i = 0; i < m; i++) {
int a = find(edges[i][0]);
int b = find(edges[i][1]);
int v = wt(edges[i]);
if (a != b) {
savings.push_back(v-merge(a, b)-mn);
s += v;
}
}
sort(savings.rbegin(), savings.rend());
cout << s << '\n';
for (int v = 0; v < q; v++) {
s -= (v >= savings.size() ? 0 : savings[v]);
cout << s << '\n';
}
}
Compilation message (stderr)
guard.cpp: In function 'int main()':
guard.cpp:68:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | s -= (v >= savings.size() ? 0 : savings[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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |