Submission #1111823

#TimeUsernameProblemLanguageResultExecution timeMemory
1111823ezzzaySjekira (COCI20_sjekira)C++14
0 / 110
65 ms21432 KiB
#include<bits/stdc++.h> using namespace std; #define int long long #define ff first #define ss second #define pb push_back const int N=3e5+5; int par[N],c[N],sbtr[N],mx[N]; vector<int>v[N]; int ans=0; bool vis[N]; int find(int x){ if(x==par[x])return x; return par[x]=find(par[x]); } void merge(int x, int y){ int px=find(x),py=find(y); if(sbtr[px]<sbtr[py]){ swap(py,px); } ans+=mx[px]+mx[py]; sbtr[px]+=sbtr[py]; mx[px]=max(mx[px],mx[py]); par[py]=px; } signed main(){ int n; cin>>n; vector<pair<int,int>>vc; for(int i=1;i<=n;i++){ cin>>c[i]; par[i]=i; sbtr[i]=1; mx[i]=c[i]; vc.pb({c[i],i}); } for(int i=1;i<n;i++){ int a,b; cin>>a>>b; v[a].pb(b); v[b].pb(a); } sort(vc.begin(),vc.end()); for(auto p:vc){ int a=p.ss; for(auto b:v[a]){ if(vis[b]==0){ merge(a,b); } } vis[a]=1; } cout<<ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...