#define pb push_back #define mp make_pair #define fi first #define se second #define all(...) begin(__VA_ARGS__) , end(__VA_ARGS__) #define boost {ios_base::sync_with_stdio(false); cin.tie(); cout.tie();} #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector <int> vi; typedef pair<int,int> PII; typedef pair<ll,ll> PLL; constexpr ll nax = 2e5+6969, INF = 2e16+6969; int qq,n,rep[nax],ile[nax]; ll ans[nax],t[nax],T[nax]; vector <PII> E; void init() { for(int i=0;i<=n;++i) { rep[i] = i; ile[i] = 1; t[i] = T[i]; } } int Find(int a) { if(rep[a] != a) rep[a] = Find(rep[a]); return rep[a]; } void Union(int a, int b) { a = Find(a); b = Find(b); if(a == b) return; if(ile[a] < ile[b]) swap(a,b); rep[b] = a; ile[a] += ile[b]; t[a] += t[b]; } int main() { scanf("%d", &qq); while(qq--) { scanf("%d", &n); E.clear(); for(int i=1;i<=n;++i) { scanf("%lld", &T[i]); ans[i] = INF; } for(int i=0;i<n-1;++i) { int x,y; scanf("%d%d", &x, &y); E.pb(mp(x,y)); } for(ll i=0;i<(1<<(n-1));++i) { int pos = 0; init(); for(int j=0;j<n-1;++j) { if(i&(1<<j)) Union(E[j].fi, E[j].se); } ll tmp = 0; for(int j=1;j<=n;++j) { if(rep[j] == j) { pos++; tmp += t[j]*t[j]; } } ans[pos] = min(ans[pos], tmp); } for(int i=1;i<=n;++i) printf("%lld ", ans[i]); puts(""); } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | #define pb push_back #define mp make_pair #define fi first #define se second #define all(...) begin(__VA_ARGS__) , end(__VA_ARGS__) #define boost {ios_base::sync_with_stdio(false); cin.tie(); cout.tie();} #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector <int> vi; typedef pair<int,int> PII; typedef pair<ll,ll> PLL; constexpr ll nax = 2e5+6969, INF = 2e16+6969; int qq,n,rep[nax],ile[nax]; ll ans[nax],t[nax],T[nax]; vector <PII> E; void init() { for(int i=0;i<=n;++i) { rep[i] = i; ile[i] = 1; t[i] = T[i]; } } int Find(int a) { if(rep[a] != a) rep[a] = Find(rep[a]); return rep[a]; } void Union(int a, int b) { a = Find(a); b = Find(b); if(a == b) return; if(ile[a] < ile[b]) swap(a,b); rep[b] = a; ile[a] += ile[b]; t[a] += t[b]; } int main() { scanf("%d", &qq); while(qq--) { scanf("%d", &n); E.clear(); for(int i=1;i<=n;++i) { scanf("%lld", &T[i]); ans[i] = INF; } for(int i=0;i<n-1;++i) { int x,y; scanf("%d%d", &x, &y); E.pb(mp(x,y)); } for(ll i=0;i<(1<<(n-1));++i) { int pos = 0; init(); for(int j=0;j<n-1;++j) { if(i&(1<<j)) Union(E[j].fi, E[j].se); } ll tmp = 0; for(int j=1;j<=n;++j) { if(rep[j] == j) { pos++; tmp += t[j]*t[j]; } } ans[pos] = min(ans[pos], tmp); } for(int i=1;i<=n;++i) printf("%lld ", ans[i]); puts(""); } return 0; } |