Submission #863837

#TimeUsernameProblemLanguageResultExecution timeMemory
863837vjudge1Sjekira (COCI20_sjekira)C++17
110 / 110
41 ms13456 KiB
#include<bits/stdc++.h> #define fi first #define se second using namespace std; using ll=long long; int n; pair<int,int> w[100005]; vector<int>v[100005]; bool cmp(pair<int,int>a,pair<int,int>b) { return a.fi<b.fi; } int pa[100005],ma[100005],sz[1000005]; int findd(int u) { if(pa[u]!=u) pa[u]=findd(pa[u]); return pa[u]; } ll ans=0; void uni(int a,int b) { a=findd(a);b=findd(b); //cout<<a<<" "<<b<<endl; if(a!=b) { if(sz[a]>sz[b]) swap(a,b); pa[a]=b; sz[b]+=sz[a]; ans=(ans+ma[a]+ma[b]); ma[b]=max(ma[b],ma[a]); } } bool t[100005]; int main() { cin.tie(0)->sync_with_stdio(0); //freopen(".inp","r",stdin); //freopen(".out","w",stdout); cin>>n; for(int i=1;i<=n;i++) cin>>w[i].fi; for(int i=1;i<=n;i++) w[i].se=i; for(int i=1;i<n;i++) { int x,y;cin>>x>>y; v[x].push_back(y); v[y].push_back(x); } for(int i=1;i<=n;i++) ma[i]=w[i].fi; sort(w+1,w+n+1,cmp); for(int i=1;i<=n;i++) pa[i]=i,sz[i]=1; t[w[1].se]=true; for(int i=2;i<=n;i++) { for(int j:v[w[i].se]) if(t[j]) uni(w[i].se,j); t[w[i].se]=true; } cout<<ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...