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 endl '\n'
#define int long long int
#define ins insert
#define pb push_back
#define putr(x) cout<<x<<endl;return;
#define all(x) x.begin(),x.end()
#define _ << " " <<
const int mod = 1e9+7 ,
sze= 1e5 + 23 ,
inf = 2e14,
L = 31 ;
int par[sze];
int sz[sze];
int cc[sze];
int root(int node){
if(par[node]==node){
return node;
}
return par[node]=root(par[node]);
}
int ans=0;
void unite(int a,int b){
int x = root(a);
int y = root(b);
if(x!=y){
ans+=cc[x] + cc[y];
if(sz[y]>sz[x]){
swap(x,y);
}
cc[x]=max(cc[x],cc[y]);
par[y]=x;
sz[x]+=sz[y];
}
}
void opt1z(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>cc[i];
par[i]=i;
sz[i]=i;
}
vector<pair<int,pair<int,int>>> edges;
for(int i=1;i<=n-1;i++){
int u,v;cin>>u>>v;
edges.pb({max(cc[u],cc[v]),{u,v}});
}
sort(all(edges));
for(auto [k,e]:edges){
// cout<<e.first<<" "<<e.second<<endl;
unite(e.first,e.second);
}
putr(ans);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt = 1;
// cin>>tt;
while(tt--){
opt1z();
}
return 0;
}
Compilation message (stderr)
sjekira.cpp: In function 'void opt1z()':
sjekira.cpp:53:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
53 | for(auto [k,e]:edges){
| ^
# | 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... |