Submission #400400

#TimeUsernameProblemLanguageResultExecution timeMemory
400400fadi57Sjekira (COCI20_sjekira)C++14
0 / 110
65 ms460 KiB
#include<bits/stdc++.h> using namespace std; const int mxx=300+10; typedef long long ll; int inf=1e9+10; const int mod=1e9+7; int a[mxx]; ll ans=0; pair<int,int>edge[mxx]; int n;int par[mxx]; int mx[mxx]; int siz[mxx]; vector<int>adj[mxx]; void init(){ for(int i=0;i<n;i++){ par[i]=i; mx[i]=a[i]; siz[i]=1; } } int fin(int node){ if(par[node]!=node){ return par[node]=fin(par[node]); }else{ return node; } } void unit(int a,int b){ int aa=fin(a); int bb=fin(b); if(siz[a]>siz[b]){ swap(a,b); } if(aa==bb){ return; } par[aa]=bb; ans+=(mx[aa]+mx[bb]); mx[bb]=max(mx[bb],mx[aa]); return; } int main(){ cin>>n; for(int i=0;i<n;i++){ cin>>a[i]; } vector<int>v; for(int i=0;i<n-1;i++){ int x,y; cin>>x>>y; x--;y--; adj[x].push_back(y); adj[y].push_back(x); edge[i]={x,y}; v.push_back(i); } init(); ll ans2=inf; //cout<<fin(2); unit(1,2); //cout<<fin(2); int c=-1; while(next_permutation(v.begin(),v.end())){ ans=0; init(); for(int i=v.size()-1;i>=0;i--){ unit(edge[v[i]].first,edge[v[i]].second); } if(c==-1){ c=1; ans2=ans; }else{ ans2=min(ans2,ans);} } cout<<ans2; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...