//#pragma comment(linker, "/stack:200000000") #ifndef LOCAL #pragma GCC optimize("O3") #endif //#pragma GCC optimize("Ofast") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define var(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define pb push_back #define mp make_pair #define st first #define nd second #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define un(x) x.erase(unique(ALL(x)),x.end()) #define REP(i,x,v)for(int i=x;i<=v;i++) #define REPD(i,x,v)for(int i=x;i>=v;i--) #define FOR(i,v)for(int i=0;i<v;i++) #define TRAV(i,v)for(auto &i:(v)) #define REMIN(x,v) x=min(x,v); #define REMAX(x,v) x=max(x,v); #define INF (1LL<<60) mt19937_64 rng(13469); int rd(int l, int r) {return uniform_int_distribution<int>(l, r)(rng);} using ll=long long; using pii=pair<int,int>; using pll=pair<ll,ll>; using vi=vector<int>; using vpi=vector<pii>; using vll=vector<ll>; const int MOD=1e9+7;//998244353; vi kr[305]; using dpres=vector<vector<pair<ll,int>>>; bool por(const pair<ll, int> & A, const pair<ll, int> & B ) { return A.nd < B.nd; } dpres combine(dpres U, dpres V) { dpres r; FOR(i, SZ(U)) FOR(j, SZ(V)) { int k=i+j; while(SZ(r)<k+2) { r.pb({}); } FOR(ii, SZ(U[i])) FOR(jj, SZ(V[j])) { pair<ll, int> z = {U[i][ii].st + V[j][jj].st, U[i][ii].nd + V[j][jj].nd}; r[k].pb(z); if (V[j][jj].nd) { pair<ll, int> w = {U[i][ii].st + V[j][jj].st + V[j][jj].nd*1LL*V[j][jj].nd, U[i][ii].nd}; r[k+1].pb(w); } } } while(SZ(r) && SZ(r.back())==0) r.pop_back(); FOR(i, SZ(r)) { sort(ALL(r[i]), por); vector<pair<ll,int>> red; for(auto pa: r[i]) { if (SZ(red)==0) { red.pb(pa);continue; } if (pa.st+pa.nd*1LL*pa.nd < red.back().st +red.back().nd*1LL*red.back().nd) { red.pb(pa); } } r[i]=red; } return r; } dpres dp[303]; int treesize[303]; int treeh[303]; ll treesum[303]; int A[303]; void dfs_compute_stuff(int u) { for(auto v: kr[u]) dfs_compute_stuff(v); treesize[u]=1; treesum[u]=A[u]; treeh[u]=1; for(auto v:kr[u]) { treesize[u]+=treesize[v]; treesum[u]+=treesum[v]; REMAX(treeh[u], 1+treeh[v]); } } void dfs(int u) { for(auto v: kr[u]) dfs(v); dpres r; r.pb({{0,A[u]}}); for(auto v:kr[u]) { r = combine(r, dp[v]); } dp[u]=r; } void dfs_rem_par(int u, int p) { for(auto v:kr[u]) { if (v!=p) dfs_rem_par(v, u); } FOR(i, SZ(kr[u])) { if (kr[u][i]==p) { swap(kr[u][i], kr[u].back()); kr[u].pop_back(); break; } } } void populate_graph(int n, vpi edges, int root) { FOR(i,n) kr[i].clear(); FOR(i,n-1) kr[edges[i].st].pb(edges[i].nd); FOR(i,n-1) kr[edges[i].nd].pb(edges[i].st); dfs_rem_par(root, -1); dfs_compute_stuff(root); } vector<ll> sol(int n, vpi edges, vi a) { int root=0; int besth=0; FOR(i,n) { populate_graph(n, edges, i); if (treeh[i]>besth) { besth=treeh[i]; root=i; } } populate_graph(n, edges, root); FOR(i,n) A[i]=a[i]; dfs(root); vector<ll> be(n); FOR(k, n) { ll best=INF; for(auto pa: dp[root][k]) { REMIN(best, pa.st + pa.nd*1LL*pa.nd); } be[k]=best; } return be; } int main() { int te;cin>>te; FOR(t, te) { int n;cin>>n; vector<pii> ed; vi a(n); FOR(i,n) cin>>a[i]; FOR(i,n-1) { int x,y;cin>>x>>y; x--;y--; ed.pb({x,y}); } vector<ll> be=sol(n, ed, a); FOR(i,n) cout<<be[i]<<" "; cout<<"\n"; } 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 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | //#pragma comment(linker, "/stack:200000000") #ifndef LOCAL #pragma GCC optimize("O3") #endif //#pragma GCC optimize("Ofast") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define var(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define pb push_back #define mp make_pair #define st first #define nd second #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define un(x) x.erase(unique(ALL(x)),x.end()) #define REP(i,x,v)for(int i=x;i<=v;i++) #define REPD(i,x,v)for(int i=x;i>=v;i--) #define FOR(i,v)for(int i=0;i<v;i++) #define TRAV(i,v)for(auto &i:(v)) #define REMIN(x,v) x=min(x,v); #define REMAX(x,v) x=max(x,v); #define INF (1LL<<60) mt19937_64 rng(13469); int rd(int l, int r) {return uniform_int_distribution<int>(l, r)(rng);} using ll=long long; using pii=pair<int,int>; using pll=pair<ll,ll>; using vi=vector<int>; using vpi=vector<pii>; using vll=vector<ll>; const int MOD=1e9+7;//998244353; vi kr[305]; using dpres=vector<vector<pair<ll,int>>>; bool por(const pair<ll, int> & A, const pair<ll, int> & B ) { return A.nd < B.nd; } dpres combine(dpres U, dpres V) { dpres r; FOR(i, SZ(U)) FOR(j, SZ(V)) { int k=i+j; while(SZ(r)<k+2) { r.pb({}); } FOR(ii, SZ(U[i])) FOR(jj, SZ(V[j])) { pair<ll, int> z = {U[i][ii].st + V[j][jj].st, U[i][ii].nd + V[j][jj].nd}; r[k].pb(z); if (V[j][jj].nd) { pair<ll, int> w = {U[i][ii].st + V[j][jj].st + V[j][jj].nd*1LL*V[j][jj].nd, U[i][ii].nd}; r[k+1].pb(w); } } } while(SZ(r) && SZ(r.back())==0) r.pop_back(); FOR(i, SZ(r)) { sort(ALL(r[i]), por); vector<pair<ll,int>> red; for(auto pa: r[i]) { if (SZ(red)==0) { red.pb(pa);continue; } if (pa.st+pa.nd*1LL*pa.nd < red.back().st +red.back().nd*1LL*red.back().nd) { red.pb(pa); } } r[i]=red; } return r; } dpres dp[303]; int treesize[303]; int treeh[303]; ll treesum[303]; int A[303]; void dfs_compute_stuff(int u) { for(auto v: kr[u]) dfs_compute_stuff(v); treesize[u]=1; treesum[u]=A[u]; treeh[u]=1; for(auto v:kr[u]) { treesize[u]+=treesize[v]; treesum[u]+=treesum[v]; REMAX(treeh[u], 1+treeh[v]); } } void dfs(int u) { for(auto v: kr[u]) dfs(v); dpres r; r.pb({{0,A[u]}}); for(auto v:kr[u]) { r = combine(r, dp[v]); } dp[u]=r; } void dfs_rem_par(int u, int p) { for(auto v:kr[u]) { if (v!=p) dfs_rem_par(v, u); } FOR(i, SZ(kr[u])) { if (kr[u][i]==p) { swap(kr[u][i], kr[u].back()); kr[u].pop_back(); break; } } } void populate_graph(int n, vpi edges, int root) { FOR(i,n) kr[i].clear(); FOR(i,n-1) kr[edges[i].st].pb(edges[i].nd); FOR(i,n-1) kr[edges[i].nd].pb(edges[i].st); dfs_rem_par(root, -1); dfs_compute_stuff(root); } vector<ll> sol(int n, vpi edges, vi a) { int root=0; int besth=0; FOR(i,n) { populate_graph(n, edges, i); if (treeh[i]>besth) { besth=treeh[i]; root=i; } } populate_graph(n, edges, root); FOR(i,n) A[i]=a[i]; dfs(root); vector<ll> be(n); FOR(k, n) { ll best=INF; for(auto pa: dp[root][k]) { REMIN(best, pa.st + pa.nd*1LL*pa.nd); } be[k]=best; } return be; } int main() { int te;cin>>te; FOR(t, te) { int n;cin>>n; vector<pii> ed; vi a(n); FOR(i,n) cin>>a[i]; FOR(i,n-1) { int x,y;cin>>x>>y; x--;y--; ed.pb({x,y}); } vector<ll> be=sol(n, ed, a); FOR(i,n) cout<<be[i]<<" "; cout<<"\n"; } return 0; } |