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 base 31
#define fi first
#define endl "\n"
#define se second
#define NAME "FILE"
#define ll long long
#define int long long
#define mod 1000000007
#define pii pair<int,int>
#define bit(mask,i) (mask&(1<<i))
#define lcm(a,b) ((a*b)/__gcd(a,b))
#define turn_on(mask,i) (mask|(1<<i))
#define turn_off(mask,i) (mask^(1<<i))
template <class T1, class T2>
bool maximize(T1 &a, T2 b){if (a < b){a = b; return 1;} return 0;}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){if (a > b){a = b; return 1;} return 0;}
template <class T1, class T2>
void add(T1 &a, T2 b){a += b; if (a >= mod) a -= mod;}
template <class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if (a < 0) a += mod;}
using namespace std;
int sz[100010],mx[100010],res;
int find(int u){ return sz[u]<0?u:sz[u]=find(sz[u]);}
void Union(int u,int v){
u=find(u);v=find(v);
if(u==v)return ;
res+=mx[u]+mx[v];
if(sz[u]>sz[v])swap(u,v);
sz[u]+=sz[v];
sz[v]=u;
maximize(mx[u],mx[v]);
}
int n;
vector<pair<int,pii>>q;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
if (fopen(NAME".inp","r")){
freopen(NAME".inp","r",stdin);
freopen(NAME".out","w",stdout);
}
memset(sz,-1,sizeof sz);
cin>>n;
for(int i=1;i<=n;i++)cin>>mx[i];
for(int i=1,u,v;i<n;i++){
cin>>u>>v;
q.push_back({max(mx[u],mx[v]),{u,v}});
}
sort(q.begin(),q.end());
for(auto x:q){
int u=x.se.fi,v=x.se.se;
Union(u,v);
}
cout<<res;
}
Compilation message (stderr)
sjekira.cpp: In function 'int main()':
sjekira.cpp:46:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | freopen(NAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
sjekira.cpp:47:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | freopen(NAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |