Submission #1111826

#TimeUsernameProblemLanguageResultExecution timeMemory
1111826ezzzaySjekira (COCI20_sjekira)C++14
110 / 110
81 ms12520 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]); } 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,pair<int,int>>>vc; for(int i=1;i<n;i++){ int a,b; cin>>a>>b; vc.pb({max(c[a],c[b]),{a,b}}); } sort(vc.begin(),vc.end()); for(auto p:vc){ int a=p.ss.ff,b=p.ss.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...