#include<bits/stdc++.h> using namespace std; vector<int> pol[305]; int war[305]; bool odw[305]; long long mini[305]; int licz = 0; void dfs(int a) { odw[a] = true; licz += war[a]; for(auto x:pol[a]) if(!odw[x]) dfs(x); } void solve() { int a; cin>>a; vector<pair<int,int>> v; for(int x=1;x<=a;x++) { cin>>war[x]; mini[x] = 1e18; } for(int x=0;x<a-1;x++) { int c,d; cin>>c>>d; v.push_back(make_pair(c,d)); } for(int x=0;x<(1<<v.size());x++) { int pom = x; for(auto y:v) { if(pom&1) { pol[y.first].push_back(y.second); pol[y.second].push_back(y.first); } pom/=2; } long long siema = 0; for(int x=1;x<=a;x++) if(!odw[x]) { dfs(x); siema += (long long)licz*licz; licz = 0; } for(int x=1;x<=a;x++) { pol[x].resize(0); odw[x]=false; } mini[a - __builtin_popcount(x)] = min(mini[a - __builtin_popcount(x)] , siema); } for(int x=1;x<=a;x++) cout<<mini[x]<<" "; cout<<'\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin>>t; while(t--) solve(); 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 | #include<bits/stdc++.h> using namespace std; vector<int> pol[305]; int war[305]; bool odw[305]; long long mini[305]; int licz = 0; void dfs(int a) { odw[a] = true; licz += war[a]; for(auto x:pol[a]) if(!odw[x]) dfs(x); } void solve() { int a; cin>>a; vector<pair<int,int>> v; for(int x=1;x<=a;x++) { cin>>war[x]; mini[x] = 1e18; } for(int x=0;x<a-1;x++) { int c,d; cin>>c>>d; v.push_back(make_pair(c,d)); } for(int x=0;x<(1<<v.size());x++) { int pom = x; for(auto y:v) { if(pom&1) { pol[y.first].push_back(y.second); pol[y.second].push_back(y.first); } pom/=2; } long long siema = 0; for(int x=1;x<=a;x++) if(!odw[x]) { dfs(x); siema += (long long)licz*licz; licz = 0; } for(int x=1;x<=a;x++) { pol[x].resize(0); odw[x]=false; } mini[a - __builtin_popcount(x)] = min(mini[a - __builtin_popcount(x)] , siema); } for(int x=1;x<=a;x++) cout<<mini[x]<<" "; cout<<'\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin>>t; while(t--) solve(); return 0; } |