Submission #1030895

#TimeUsernameProblemLanguageResultExecution timeMemory
1030895UnforgettableplSecurity Guard (JOI23_guard)C++17
50 / 100
127 ms23272 KiB
#include <bits/stdc++.h> using namespace std; #define int long long struct UFDS{ vector<int> p,siz; UFDS(int n):p(n+1),siz(n+1,1){iota(p.begin(),p.end(),0);} int findRoot(int x){ return p[x]==x ? x : p[x]=findRoot(p[x]); } 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); siz[a]+=siz[b]; p[b]=a; 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 ans = (*max_element(arr.begin(),arr.end())); for(int i=1;i<=n;i++){ ans-=arr[i]; } UFDS dsu(n); for(int i=0;i<m;i++){ auto [cost,a,b] = edges[i]; if(dsu.unite(a,b)){ ans+=cost; } } cout << ans << '\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...