Submission #1111817

#TimeUsernameProblemLanguageResultExecution timeMemory
1111817ezzzaySjekira (COCI20_sjekira)C++14
40 / 110
1049 ms16640 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]; int ans=0; int find(int x){ if(x==par[x])return x; return par[x]=find(par[x]); } bool vis[N]; 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; for(int i=1;i<=n;i++){ cin>>c[i]; par[i]=i; sbtr[i]=1; mx[i]=c[i]; } vector<pair<int,int>>vc; for(int i=1;i<n;i++){ int a,b; cin>>a>>b; vc.pb({a,b}); } while(1){ vector<pair<int,int>>v; for(int i=0;i<n-1;i++){ if(vis[i]==0){ int a=vc[i].ff,b=vc[i].ss; int px=find(a); int py=find(b); v.pb({(mx[px]+mx[py]),i}); } } if(v.size()==0)break; sort(v.begin(),v.end()); int idx=v[0].ss; vis[idx]=1; int a=vc[idx].ff,b=vc[idx].ss; merge(a,b); } 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...