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 mxN = (int)1e5 + 5;
vector<pair<int64_t,pair<int,int>>>e;
vector<pair<int64_t,pair<int,int>>>v;
int n,m;
vector<int>parent(mxN);
int UF(int u) {
if(parent[u] == u)
return u;
return parent[u] = UF(parent[u]);
}
int64_t mst(int64_t X) {
for(int i = 0 ; i < m ; i++)
v[i].first = abs(e[i].first - X), v[i].second = e[i].second;
sort(v.begin(),v.end());
int cnt = 0;
int p = 0;
int64_t ans = 0;
vector<bool>vis(n,0);
while(cnt < n-1) {
int A = UF(v[p].second.first),
B = UF(v[p].second.second);
if(A == B) {
p++;
continue;
}
ans += v[p].first;
cnt++;
parent[A] = B;
p++;
}
return ans;
}
void go() {
for(int i = 0 ; i < n ; i++)
parent[i] = i;
}
signed main() {
cin>>n>>m;
for(int i = 0 ; i < m ; i++) {
int64_t a,b,c; cin>>a>>b>>c;
a--,b--;
e.push_back({c,{a,b}});
}
v.resize(m);
int q; cin>>q;
while(q--) {
int64_t X; cin>>X;
go();
cout << mst(X) << "\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... |