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;
using ll = long long;
struct dsu{
vector<ll> p;
dsu(ll N):p(N,-1){}
ll g(ll x){return p[x]==-1?x:p[x]=g(p[x]);}
bool m(ll a, ll b){
a=g(a);b=g(b);
if(a!=b)p[a]=b;
return a!=b;
}
};
struct edge{
ll u,v,w;
bool operator<(const edge& o) const{
return w<o.w;
}
};
int main(){
ios::sync_with_stdio(0);cin.tie(0);
ll n,m;
cin >> n >> m;
vector<edge> a(m);
for(edge &o : a) cin >> o.u >> o.v >> o.w, o.u--,o.v--;
ll q;
cin >> q;
while(q--){
ll x;
cin >> x;
vector<edge> b = a;
for(edge &o : b) o.w = abs(x-o.w);
sort(b.begin(),b.end());
ll ans = 0;
dsu d(n);
for(auto [u,v,w] : b){
ans += w*d.m(u,v);
}
cout << ans << "\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... |