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
struct UFDS{
vector<int> p,siz,mini;
multiset<int> miniset;
int sigma,compo;
UFDS(int n,vector<int> arr):p(n+1),siz(n+1,1),mini(n+1),sigma(0),compo(n){
iota(p.begin(),p.end(),0);
mini=arr;
for(int i=1;i<=n;i++){
miniset.insert(arr[i]);
sigma+=arr[i];
}
}
int findRoot(int x){
return p[x]==x ? x : p[x]=findRoot(p[x]);
}
int getans(){
return sigma+(compo-2ll)*(*miniset.begin());
}
bool unite(int a,int b){
a = findRoot(a);
b = findRoot(b);
if(a==b)return false;
if(siz[b]>siz[a])swap(a,b);
int maxi = max(mini[a],mini[b]);
mini[a]=min(mini[a],mini[b]);
miniset.erase(miniset.find(maxi));
sigma-=maxi;
siz[a]+=siz[b];
p[b]=a;
compo--;
return true;
}
};
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,m,q;
cin >> n >> m >> q;
vector<int> arr(n+1);
for(int i=1;i<=n;i++)cin>>arr[i];
vector<tuple<int,int,int>> edges;
for(int i=1;i<=m;i++){
int a,b;
cin >> a >> b;
edges.emplace_back(arr[a]+arr[b],a,b);
}
sort(edges.begin(),edges.end());
int baseans = (*max_element(arr.begin(),arr.end()));
for(int i=1;i<=n;i++){
baseans-=arr[i];
}
UFDS dsu(n,arr);
vector<int> ans(n);
ans[0] = dsu.getans();
int iter = 1;
int offset = 0;
for(int i=0;i<m;i++){
auto [cost,a,b] = edges[i];
if(dsu.unite(a,b)){
offset+=cost;
ans[iter++]=offset+dsu.getans();
}
}
for(int i=0;i<=q;i++){
cout << baseans+ans[max(0ll,n-1ll-i)] << '\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... |