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 N=2e5+5;
using ll = long long;
using T = tuple<ll,int,int>;
using U = tuple<ll,int,int,int>;
int n,m,q;
int s[N],deg[N];
int rt;
ll cur;
vector<T> edge;
priority_queue<T,vector<T>,greater<T>> comp[N];
priority_queue<U,vector<U>,greater<U>> pq;
bool good[N];
vector<ll> ans;
struct DSU{
int p[N];
void init(){
for(int i=1;i<=n;i++)p[i]=i;
}
int fp(int u){
return p[u]=u==p[u]?u:fp(p[u]);
}
bool uni(int u,int v){
u=fp(u),v=fp(v);
return u!=v?p[u]=v,true:false;
}
}dsu;
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m >> q;
for(int i=1;i<=n;i++){
cin >> s[i];
}
rt=min_element(s+1,s+n+1)-s;
cur+=(n-2)*s[rt]+*max_element(s+1,s+n+1);
for(int i=0;i<m;i++){
int u,v;
cin >> u >> v;
edge.emplace_back(s[u]+s[v],u,v);
}
sort(edge.begin(),edge.end());
dsu.init();
for(auto [w,u,v]:edge)if(dsu.uni(u,v)){
if(u==rt||v==rt)good[u]=good[v]=true;
else{
comp[u].emplace(w,u,v);
comp[v].emplace(w,v,u);
}
}
good[rt]=false;
dsu.init();
for(int i=1;i<=n;i++)if(!good[i]&&!comp[i].empty()){
auto [w,u,v]=comp[i].top();
pq.emplace(w-(s[rt]+s[u]),u,v,i);
}
ans.emplace_back(cur);
while(!pq.empty()){
auto [w,u,v,p]=pq.top();
pq.pop();
u=dsu.fp(u),v=dsu.fp(v);
if(u==v||u!=p)continue;
cur+=w;
ans.emplace_back(cur);
if(comp[u].size()>comp[v].size())swap(comp[u],comp[v]);
dsu.p[u]=v;
while(!comp[u].empty()){
comp[v].emplace(comp[u].top());
comp[u].pop();
}
if(good[v])continue;
while(!comp[v].empty()){
auto [c,a,b]=comp[v].top();
if(dsu.fp(a)!=dsu.fp(b))break;
comp[v].pop();
}
if(!comp[v].empty()){
auto [c,a,b]=comp[v].top();
pq.emplace(c-(s[rt]+s[v]),a,b,v);
}
}
reverse(ans.begin(),ans.end());
for(int i=0;i<=q;i++)cout << ans[min(i,(int)ans.size()-1)] << "\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... |