제출 #445906

#제출 시각아이디문제언어결과실행 시간메모리
445906JasiekstrzSjekira (COCI20_sjekira)C++17
110 / 110
92 ms11648 KiB
#include<bits/stdc++.h> #define fi first #define se second using namespace std; const int N=1e5; int tab[N+10]; vector<pair<int,int>> e[N+10]; bool vis[N+10]; priority_queue<pair<int,int>> pq; vector<pair<int,int>> ord; int fau[N+10]; int f(int x) { return (fau[x]==x ? x:fau[x]=f(fau[x])); } void u(int x,int y) { x=f(x); y=f(y); if(tab[x]>tab[y]) fau[y]=x; else fau[x]=y; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin>>n; for(int i=1;i<=n;i++) { cin>>tab[i]; pq.push({tab[i],i}); fau[i]=i; } for(int i=1;i<n;i++) { int a,b; cin>>a>>b; e[a].emplace_back(b,i); e[b].emplace_back(a,i); } long long ans=0; while(!pq.empty()) { int x=pq.top().se; pq.pop(); for(auto [v,id]:e[x]) { if(vis[id]) continue; vis[id]=true; ord.emplace_back(x,v); } } reverse(ord.begin(),ord.end()); for(auto [a,b]:ord) { ans+=tab[f(a)]+tab[f(b)]; u(a,b); } cout<<ans<<"\n"; 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...