Submission #1030911

#TimeUsernameProblemLanguageResultExecution timeMemory
1030911UnforgettableplSecurity Guard (JOI23_guard)C++17
50 / 100
108 ms19632 KiB
#include <bits/stdc++.h> using namespace std; #define int long long struct UFDS{ vector<int> p,siz,mini; 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++){ sigma+=arr[i]; } } int findRoot(int x){ return p[x]==x ? x : p[x]=findRoot(p[x]); } int getans(int minima){ return sigma+(compo-2ll)*minima; } 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]); 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); int minima = *min_element(arr.begin()+1,arr.end()); ans[0] = dsu.getans(minima); 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(minima); } } for(int i=0;i<=q;i++){ cout << baseans+ans[max(0ll,n-1ll-i)] << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...