#include<bits/stdc++.h> using namespace std; ostream& operator<<(ostream &out, string str) { for(char c : str) out << c; return out; } template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.first << ", " << p.second << ")"; } template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << "{"; for(auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << "}"; } void dump() { cerr << "\n"; } template<class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #ifdef DEBUG # define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else # define debug(...) false #endif #define REP(i, n) for(int i = 0; i < n; i++) #define FOR(i, a, b) for(int i = a; i <= b; i++) #define ST first #define ND second template<class T> int size(T && a) { return (int) a.size(); } using LL = long long; using PII = pair<int, int>; vector<int> a, sub; vector<vector<int>> adj; using T = vector<pair<int, LL>>; vector<vector<T>> dp; LL square(LL x) { return x * x; } void upkeep(T &nt) { sort(nt.begin(), nt.end()); LL bound = 1e18, bound2 = 1e18; T ret; for(auto &[x, y] : nt) { if(y < bound && square(x) + y < bound2) { bound = y, bound2 = square(x) + y; ret.emplace_back(x, y); } } swap(nt, ret); } void upkeep(vector<T> &nt) { for(auto &v : nt) upkeep(v); } void dfs(int v = 0, int p = -1) { sub[v] = 1; dp[v] = { {{a[v], 0}}, {}}; for(int u : adj[v]) { if(u != p) { dfs(u, v); vector<T> nt(sub[v] + sub[u] + 1); REP(i, sub[v]) { REP(j, sub[u] + 1) { auto x = dp[v][i], y = dp[u][j]; for(auto &[sx, cx] : x) for(auto &[sy, cy] : y) nt[i + j].emplace_back(sx + sy, cx + cy); } } sub[v] += sub[u]; swap(dp[v], nt); upkeep(dp[v]); } } REP(i, sub[v]) for(auto &[s, c] : dp[v][i]) dp[v][i + 1].emplace_back(0, square(s) + c); upkeep(dp[v]); REP(i, sub[v] + 1) debug(v, i, dp[v][i]); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; REP(_t, t) { int n; cin >> n; a = vector<int>(n); REP(i, n) cin >> a[i]; adj = vector<vector<int>>(n); REP(i, n - 1) { int x, y; cin >> x >> y; adj[x - 1].emplace_back(y - 1); adj[y - 1].emplace_back(x - 1); } sub = vector<int>(n); dp.clear(); dp.resize(n); dfs(); FOR(i, 1, n) cout << dp[0][i][0].ND << " "; 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 | #include<bits/stdc++.h> using namespace std; ostream& operator<<(ostream &out, string str) { for(char c : str) out << c; return out; } template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.first << ", " << p.second << ")"; } template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << "{"; for(auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << "}"; } void dump() { cerr << "\n"; } template<class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #ifdef DEBUG # define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else # define debug(...) false #endif #define REP(i, n) for(int i = 0; i < n; i++) #define FOR(i, a, b) for(int i = a; i <= b; i++) #define ST first #define ND second template<class T> int size(T && a) { return (int) a.size(); } using LL = long long; using PII = pair<int, int>; vector<int> a, sub; vector<vector<int>> adj; using T = vector<pair<int, LL>>; vector<vector<T>> dp; LL square(LL x) { return x * x; } void upkeep(T &nt) { sort(nt.begin(), nt.end()); LL bound = 1e18, bound2 = 1e18; T ret; for(auto &[x, y] : nt) { if(y < bound && square(x) + y < bound2) { bound = y, bound2 = square(x) + y; ret.emplace_back(x, y); } } swap(nt, ret); } void upkeep(vector<T> &nt) { for(auto &v : nt) upkeep(v); } void dfs(int v = 0, int p = -1) { sub[v] = 1; dp[v] = { {{a[v], 0}}, {}}; for(int u : adj[v]) { if(u != p) { dfs(u, v); vector<T> nt(sub[v] + sub[u] + 1); REP(i, sub[v]) { REP(j, sub[u] + 1) { auto x = dp[v][i], y = dp[u][j]; for(auto &[sx, cx] : x) for(auto &[sy, cy] : y) nt[i + j].emplace_back(sx + sy, cx + cy); } } sub[v] += sub[u]; swap(dp[v], nt); upkeep(dp[v]); } } REP(i, sub[v]) for(auto &[s, c] : dp[v][i]) dp[v][i + 1].emplace_back(0, square(s) + c); upkeep(dp[v]); REP(i, sub[v] + 1) debug(v, i, dp[v][i]); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; REP(_t, t) { int n; cin >> n; a = vector<int>(n); REP(i, n) cin >> a[i]; adj = vector<vector<int>>(n); REP(i, n - 1) { int x, y; cin >> x >> y; adj[x - 1].emplace_back(y - 1); adj[y - 1].emplace_back(x - 1); } sub = vector<int>(n); dp.clear(); dp.resize(n); dfs(); FOR(i, 1, n) cout << dp[0][i][0].ND << " "; cout << "\n"; } } |