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;
long long N,M,Q, ufds[505], rnk[505];
tuple<long long,long long,long long> A[100005], B[100005];
long long findSet(long long i) {
return ufds[i] == i ? i : ufds[i] = findSet(ufds[i]);
}
void unionSet(long long i, long long j) {
long long a = findSet(i), b = findSet(j);
if (a == b) return;
if (rnk[a] < rnk[b]) ufds[a] = b;
if (rnk[a] > rnk[b]) ufds[b] = a;
if (rnk[a] == rnk[b]) ufds[a] = b, rnk[b]++;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> N >> M;
for (long long i = 0; i < M; ++i) {
long long u,v,w; cin >> u >> v >> w; u--, v--;
A[i] = make_tuple(u,v,w);
}
cin >> Q;
while (Q--) {
long long x; cin >> x;
for (long long i = 0; i < M; ++i) B[i] = make_tuple(abs(x-get<2>(A[i])),get<0>(A[i]),get<1>(A[i]));
sort(B,B+M);
for (long long i = 0; i < N; ++i) ufds[i] = i, rnk[i] = 1;
long long ans = 0;
for (long long i = 0; i < M; ++i) {
long long u,v,w; tie(w,u,v) = B[i];
if (findSet(u) == findSet(v)) continue;
ans += w;
unionSet(u,v);
}
cout << ans << '\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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |