제출 #875418

#제출 시각아이디문제언어결과실행 시간메모리
875418vjudge1Sjekira (COCI20_sjekira)C++17
110 / 110
42 ms13140 KiB
#include<bits/stdc++.h> using namespace std; const int N=1e5+7; #define int long long #define fi first #define se second typedef pair<int, int> ii; int n; ii w[N]; vector<int> g[N]; int p[N], ma[N], sz[N], ans=0, cx[N]; int Find(int u){ if(u==p[u]) return u; return p[u]=Find(p[u]); } void Merge(int a, int b){ a=Find(a); b=Find(b); if(a!=b){ if(sz[a]>sz[b]) swap(a, b); p[a]=b; sz[b]+=sz[a]; ans=(ans+ma[a]+ma[b]); ma[b]=max(ma[b], ma[a]); } } int32_t main(){ cin.tie(0)->sync_with_stdio(0); cin>>n; for(int i=1; i<=n; i++){ cin>>w[i].fi; w[i].se=i; } for(int i=1; i<n; i++){ int x, y; cin>>x>>y; g[x].push_back(y); g[y].push_back(x); } for(int i=1; i<=n; i++) ma[i]=w[i].fi; sort(w+1, w+n+1); for(int i=1; i<=n; i++){ p[i]=i; sz[i]=1; } cx[w[1].se]=true; for(int i=2; i<=n; i++){ for(int j: g[w[i].se]) if(cx[j]) Merge(w[i].se, j); cx[w[i].se]=true; } 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...