#pragma GCC optimize("Ofast") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define mp make_pair #define eb emplace_back #define pb push_back #define e1 first #define e2 second #define uint unsigned int #define ll long long #define ull unsigned long long #define ld long double #define float long double #define size(x) (int)x.size() #define satori int testCases; cin>>testCases; while(testCases--) #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define all(r) begin(r),end(r) #define time chrono::high_resolution_clock().now().time_since_epoch().count() typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; mt19937 rng(chrono::high_resolution_clock().now().time_since_epoch().count()); /////////////////// #define debug if(1) /////////////////// const int MAXN=3e2+10; const ll inf=1e18+2137; int a[MAXN],aa[MAXN],id[MAXN],par[MAXN],sz[MAXN],cnt; ll b[MAXN],res[MAXN],exk[MAXN]; vector<pair<ll,ll>> hull[MAXN]; vector<ll> hullpos[MAXN]; vector<pair<ll,ll>> temp; vector<int> g[MAXN]; int r1,r2,n; void dfs(int u,int p){ sz[u]=1; for(auto v:g[u]) if(v!=p) dfs(v,u),sz[u]+=sz[v]; if(r1==-1) r1=p,r2=u; else{ if(abs(sz[r2]-(n-sz[r2]))>abs(sz[u]-(n-sz[u]))) r1=p,r2=u; } } void dfs_set(int u,int p){ id[u]=--cnt; a[id[u]]=aa[u]; for(auto v:g[u]) if(v!=p) dfs_set(v,u),par[id[v]]=id[u]; } ll get_best(ll slope,int id){ int pos=upper_bound(all(hullpos[id]),slope)-begin(hullpos[id])-1; return hull[id][pos].e1+hull[id][pos].e2*slope; } int32_t main() { fastio; satori{ int u,v; ll cres; cin>>n; for(int i=0;i<n;i++) cin>>aa[i],g[i].clear(),res[i]=inf,exk[i]=inf,hull[i].clear(),hullpos[i].clear(); for(int i=0;i<n-1;i++) cin>>u>>v,g[u-1].eb(v-1),g[v-1].eb(u-1); r1=r2=-1; dfs(0,-1); int sn=sz[r2],bits; cnt=sn; dfs_set(r2,r1); for(int m=0;m<(1<<(sn-1));m++){ cres=0; fill(b,b+sn,0ll); for(int i=0;i<sn;i++){ b[i]+=a[i]; if(m&(1<<i)) b[par[i]]+=b[i]; else cres+=b[i]*b[i]; } bits=__builtin_popcount(m); exk[bits]=min(exk[bits],cres); hull[bits].eb(mp(cres,b[sn-1])); } ll min_slope; for(int i=0;i<sn;i++){ temp.clear(); swap(temp,hull[i]); sort(all(temp)); min_slope=inf; for(auto x:temp){ if(x.e2>=min_slope) continue; else min_slope=x.e2; while(size(hull[i])&&hullpos[i].back()>=(x.e1-hull[i].back().e1+hull[i].back().e2-x.e2-1ll)/(hull[i].back().e2-x.e2)) hull[i].pop_back(),hullpos[i].pop_back(); if(size(hull[i])==0) hullpos[i].eb(0ll); else hullpos[i].eb((x.e1-hull[i].back().e1+hull[i].back().e2-x.e2-1ll)/(hull[i].back().e2-x.e2)); hull[i].eb(x); } } sn=n-sz[r2]; cnt=sn; dfs_set(r1,r2); int otsz=sz[r2]; for(int m=0;m<(1<<(sn-1));m++){ cres=0; fill(b,b+sn,0ll); for(int i=0;i<sn;i++){ b[i]+=a[i]; if(m&(1<<i)) b[par[i]]+=b[i]; else cres+=b[i]*b[i]; } bits=__builtin_popcount(m); for(int i=0;i<otsz;i++){ res[i+bits]=min(res[i+bits],cres+exk[i]); res[i+bits+1]=min(res[i+bits+1],cres+get_best(b[sn-1]*2ll,i)); } } for(int i=n-1;i>=0;i--) cout<<res[i]<<' '; cout<<'\n'; } }
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | #pragma GCC optimize("Ofast") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define mp make_pair #define eb emplace_back #define pb push_back #define e1 first #define e2 second #define uint unsigned int #define ll long long #define ull unsigned long long #define ld long double #define float long double #define size(x) (int)x.size() #define satori int testCases; cin>>testCases; while(testCases--) #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define all(r) begin(r),end(r) #define time chrono::high_resolution_clock().now().time_since_epoch().count() typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; mt19937 rng(chrono::high_resolution_clock().now().time_since_epoch().count()); /////////////////// #define debug if(1) /////////////////// const int MAXN=3e2+10; const ll inf=1e18+2137; int a[MAXN],aa[MAXN],id[MAXN],par[MAXN],sz[MAXN],cnt; ll b[MAXN],res[MAXN],exk[MAXN]; vector<pair<ll,ll>> hull[MAXN]; vector<ll> hullpos[MAXN]; vector<pair<ll,ll>> temp; vector<int> g[MAXN]; int r1,r2,n; void dfs(int u,int p){ sz[u]=1; for(auto v:g[u]) if(v!=p) dfs(v,u),sz[u]+=sz[v]; if(r1==-1) r1=p,r2=u; else{ if(abs(sz[r2]-(n-sz[r2]))>abs(sz[u]-(n-sz[u]))) r1=p,r2=u; } } void dfs_set(int u,int p){ id[u]=--cnt; a[id[u]]=aa[u]; for(auto v:g[u]) if(v!=p) dfs_set(v,u),par[id[v]]=id[u]; } ll get_best(ll slope,int id){ int pos=upper_bound(all(hullpos[id]),slope)-begin(hullpos[id])-1; return hull[id][pos].e1+hull[id][pos].e2*slope; } int32_t main() { fastio; satori{ int u,v; ll cres; cin>>n; for(int i=0;i<n;i++) cin>>aa[i],g[i].clear(),res[i]=inf,exk[i]=inf,hull[i].clear(),hullpos[i].clear(); for(int i=0;i<n-1;i++) cin>>u>>v,g[u-1].eb(v-1),g[v-1].eb(u-1); r1=r2=-1; dfs(0,-1); int sn=sz[r2],bits; cnt=sn; dfs_set(r2,r1); for(int m=0;m<(1<<(sn-1));m++){ cres=0; fill(b,b+sn,0ll); for(int i=0;i<sn;i++){ b[i]+=a[i]; if(m&(1<<i)) b[par[i]]+=b[i]; else cres+=b[i]*b[i]; } bits=__builtin_popcount(m); exk[bits]=min(exk[bits],cres); hull[bits].eb(mp(cres,b[sn-1])); } ll min_slope; for(int i=0;i<sn;i++){ temp.clear(); swap(temp,hull[i]); sort(all(temp)); min_slope=inf; for(auto x:temp){ if(x.e2>=min_slope) continue; else min_slope=x.e2; while(size(hull[i])&&hullpos[i].back()>=(x.e1-hull[i].back().e1+hull[i].back().e2-x.e2-1ll)/(hull[i].back().e2-x.e2)) hull[i].pop_back(),hullpos[i].pop_back(); if(size(hull[i])==0) hullpos[i].eb(0ll); else hullpos[i].eb((x.e1-hull[i].back().e1+hull[i].back().e2-x.e2-1ll)/(hull[i].back().e2-x.e2)); hull[i].eb(x); } } sn=n-sz[r2]; cnt=sn; dfs_set(r1,r2); int otsz=sz[r2]; for(int m=0;m<(1<<(sn-1));m++){ cres=0; fill(b,b+sn,0ll); for(int i=0;i<sn;i++){ b[i]+=a[i]; if(m&(1<<i)) b[par[i]]+=b[i]; else cres+=b[i]*b[i]; } bits=__builtin_popcount(m); for(int i=0;i<otsz;i++){ res[i+bits]=min(res[i+bits],cres+exk[i]); res[i+bits+1]=min(res[i+bits+1],cres+get_best(b[sn-1]*2ll,i)); } } for(int i=n-1;i>=0;i--) cout<<res[i]<<' '; cout<<'\n'; } } |