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 int long long
#define ar array
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, m; cin>>n>>m;
vector<int> a(m), b(m), w(m), p(m);
for(int i=0;i<m;i++){
p[i] = i;
cin>>a[i]>>b[i]>>w[i];
a[i]--, b[i]--;
}
int q; cin>>q;
while(q--){
int x; cin>>x;
sort(p.begin(), p.end(), [&](int i, int j){
return abs(w[i] - x) < abs(w[j] - x);
});
vector<int> par(n), sz(n, 1);
iota(par.begin(), par.end(), 0);
function<int(int)> f = [&](int x){
return (par[x] == x ? x : par[x] = f(par[x]));
};
auto merge = [&](int a, int b){
a = f(a), b = f(b);
if(a == b) return false;
if(sz[a] < sz[b]) swap(a, b);
sz[a] += sz[b], par[b] = a;
return true;
};
int res = 0;
for(auto i : p){
if(merge(a[i], b[i])){
res += abs(w[i] - x);
}
}
cout<<res<<"\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... |