제출 #792365

#제출 시각아이디문제언어결과실행 시간메모리
7923651075508020060209tcSjekira (COCI20_sjekira)C++14
0 / 110
1077 ms3656 KiB
#pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx,popcnt,sse4,abm") #include<bits/stdc++.h> //#pragma GCC target("popcnt") using namespace std; #define int long long #define X first #define Y second int n; int ar[500005]; int uf[500005]; int gmx[500005]; //vector<int>e[500005]; int fin(int x){ if(uf[x]==x){return x;} uf[x]=fin(uf[x]); return uf[x]; } void mrg(int a,int b){ int pa=fin(a);int pb=fin(b); if(pa==pb){return;} uf[pa]=pb; gmx[pb]=max(gmx[pb],gmx[pa]); } pair<int,int>e[500005]; signed main() { cin>>n; for(int i=1;i<=n;i++){ cin>>ar[i]; uf[i]=i; gmx[i]=ar[i]; } for(int i=1;i<=n-1;i++){ cin>>e[i].X>>e[i].Y; } int ans=0; for(int t=1;t<=n-1;t++){ int gp=fin(1); /*for(int i=1;i<=n;i++){ if(gmx[fin(i)]<gmx[gp]){ gp=fin(i); } }*/ int gp2=0; for(int i=1;i<=n-1;i++){ int a=e[i].X;int b=e[i].Y; if(fin(a)==gp&&fin(b)==gp){continue;} if(fin(a)!=gp&&fin(b)!=gp){continue;} if(fin(b)==gp){swap(a,b);} if(gp2==0||gmx[gp2]>gmx[fin(b)]){ gp2=fin(b); } } ans+=gmx[gp]+gmx[gp2]; mrg(gp,gp2); } cout<<ans<<endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...