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>
#define MAXN 200007
using namespace std;
struct edge{
int from,to,cost;
};
bool cmp(edge fr,edge sc){
return fr.cost<sc.cost;
}
int n,m,q,a,b,s[MAXN],c,d;
vector<edge> w;
unordered_set<int> tree[MAXN];
long long ans;
bool ok;
int dsu[MAXN],sz[MAXN],maxs;
int root(int x){
if(dsu[x]==x)return x;
dsu[x]=root(dsu[x]);
return dsu[x];
}
void mergev(int x,int y){
int rootx=root(x);
int rooty=root(y);
if(sz[rooty]>sz[rootx])swap(rootx,rooty);
sz[rootx]+=sz[rooty];
dsu[rooty]=dsu[rootx];
}
int best(int x,int y){
if(s[x]<s[y])return x;
return y;
}
int dfs(int x,int p){
int res=x;
for(int e:tree[x]){
if(e==p)continue;
res=best(res,dfs(e,x));
}
return res;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>q;
for(int i=1;i<=n;i++){
cin>>s[i];
ans-=s[i];
maxs=max(maxs,s[i]);
dsu[i]=i; sz[i]=1;
}
for(int i=1;i<=m;i++){
cin>>a>>b;
w.push_back({a,b,s[a]+s[b]});
}
sort(w.begin(),w.end(),cmp);
for(int i=0;i<w.size();i++){
if(root(w[i].from)==root(w[i].to))continue;
mergev(w[i].from,w[i].to);
tree[w[i].from].insert(w[i].to);
tree[w[i].to].insert(w[i].from);
ans+=s[w[i].from]+s[w[i].to];
}
ans+=maxs;
cout<<ans<<"\n";
for(int i=1;i<=q;i++){
maxs=0;
for(int f=1;f<=n and !ok;f++){
for(int e:tree[f]){
if(e<f)continue;
int sx=dfs(f,e);
int sy=dfs(e,f);
if(s[f]+s[e]-s[sx]-s[sy]>maxs){
maxs=s[f]+s[e]-s[sx]-s[sy];
a=f; b=e; c=sx; d=sy;
}
}
}
if(maxs!=0){
ans-=maxs;
tree[a].erase(b);
tree[b].erase(a);
tree[c].insert(d);
tree[d].insert(c);
}else ok=true;
cout<<ans<<"\n";
}
return 0;
}
Compilation message (stderr)
guard.cpp: In function 'int main()':
guard.cpp:75:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(int i=0;i<w.size();i++){
| ~^~~~~~~~~
# | 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... |