이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
vector<ll> x,per;
ll pa[20],ma[20];
const ll MAX=1e18;
void init(ll n){
for(ll i=0;i<n;i++){
pa[i]=i;
ma[i]=x[i];
}
}
ll findset(ll i){
if(pa[i]==i)return i;
return pa[i]=findset(pa[i]);
}
bool issameset(ll i,ll j){
return findset(i)==findset(j);
}
void unionset(ll i,ll j){
if(!issameset(i,j)){
ll u=findset(i),v=findset(j);
pa[u]=v;
ma[v]=max(ma[u],ma[v]);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll n;
cin>>n;
vector<ii> con;
map<ll,ii>no;
for(ll i=0;i<n;i++){
ll a; cin>>a;
x.push_back(a);
}
for(ll i=0;i<n-1;i++){
ll a,b;
cin>>a>>b;
a--; b--;
no[i]=ii(a,b);
per.push_back(i);
con.push_back(ii(a,b));
}
ll mi=MAX;
do{
init(n);
ll s=0;
for(ll i=0;i<n-1;i++){
ll id=per[i];
ll u=no[id].first,v=no[id].second;
s+=(ma[findset(u)]+ma[findset(v)]);
unionset(u,v);
}
mi=min(mi,s);
}while(next_permutation(per.begin(),per.end()));
cout<<mi<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |