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];
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 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;
for (int i = 0; i < n; i++) {
cin >> val[i];
s -= val[i];
mx = max(val[i], mx);
}
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);
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) {
uf[a] = b;
s += v;
}
}
cout << s << '\n';
}
# | 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... |