#include <bits/stdc++.h> using namespace std; #define PII pair<int, int> #define VI vector<int> #define VPII vector<PII> #define LL long long #define LD long double #define f first #define s second #define MP make_pair #define PB push_back #define endl '\n' // UWAGA #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for(int i = 0; i < (int)(n); ++i) #define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i) #define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i) #define random_shuffle(V) shuffle(V, rng) #define sim template<class n sim, class s> ostream & operator << (ostream &p, pair<n, s> x) {return p << "<" << x.f << ", " << x.s << ">";} sim> auto operator << (ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type {int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} sim, class s> void mini(n &p, s y) {if(p>y) p = y;} sim, class s> void maxi(n &p, s y) {if(p<y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define I(x) #x " =", (x), " " #define A(a, i) #a "[" #i " =", i, "] =", a[i], " " const int MXN = 305; #define PLL pair<LL, LL> #define VPLL vector<PLL> struct result { vector<VPLL> V; // dla każdego k pary (wielkość aktualna, wynik) int siz; result(int sizz) : V(sizz+1), siz(sizz) {} }; inline LL get(PLL &x) { return x.f * x.f + x.s; } void append(VPLL &res, PLL x) { while(res.size() && ((res.back().f >= x.f && res.back().s >= x.s) || (get(x) <= get(res.back()) && x.f <= res.back().f))) res.pop_back(); if(res.size() && ((res.back().f <= x.f && res.back().s <= x.s) || (x.f >= res.back().f && get(x) >= get(res.back())))); else res.PB(x); // assert(res.size() < 300); } // void check(VPLL &V) // { // REP(i, (int)V.size()-1) // { // assert(V[i].f <= V[i+1].f); // assert(V[i].s >= V[i+1].s); // assert(get(V[i]) >= get(V[i+1])); // } // } void merge_vec(VPLL &a, VPLL &b) // skleja dwa posortowane wektory i zapiszuje wynik w a { VPLL res; int i = 0; int j = 0; while(i < (int)a.size() || j < (int)b.size()) { if(j == (int)b.size() || (i < (int)a.size() && a[i].f < b[j].f)) append(res, a[i++]); else append(res, b[j++]); } a = res; // check(a); } result merge(result &A, result &B) { result C(A.siz + B.siz); FOR(b, 1, B.siz) { LL res = 1e18; for(auto &bb: B.V[b]) { mini(res, get(bb)); } FOR(a, 1, A.siz) { VPLL pom; for(auto &aa: A.V[a]) { pom.PB(MP(aa.f, aa.s + res)); } merge_vec(C.V[a+b], pom); } FOR(a, 1, A.siz) { VPLL pom; for(auto &bb: B.V[b]) { for(auto &aa: A.V[a]) pom.PB(MP(aa.f + bb.f, aa.s + bb.s)); } // assert(pom.size() < 6000); sort(ALL(pom)); merge_vec(C.V[a+b-1], pom); } } return C; } VI V[MXN]; int in[MXN]; int cnt = 0; result dfs(int v, int par) { vector<result> results; for(auto i: V[v]) { if(i == par)continue; auto x = dfs(i, v); results.PB(x); } result act(1); act.V[1].PB(MP(in[v], 0)); REP(j, results.size()) { act = merge(act, results[j]); } return act; } void solve() { int n; cin >> n; FOR(i, 1, n)V[i].clear(); FOR(i, 1, n)cin >> in[i]; REP(i, n-1) { int a, b; cin >> a >> b; V[a].PB(b); V[b].PB(a); } auto res = dfs(1, 0); FOR(i, 1, n) { LL val = 1e18; for(auto j: res.V[i])mini(val, j.f * j.f + j.s); cout << val << " "; } cout << endl; } int main() { ios_base::sync_with_stdio(0); int z; cin >> z; while(z--)solve(); }
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 | #include <bits/stdc++.h> using namespace std; #define PII pair<int, int> #define VI vector<int> #define VPII vector<PII> #define LL long long #define LD long double #define f first #define s second #define MP make_pair #define PB push_back #define endl '\n' // UWAGA #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for(int i = 0; i < (int)(n); ++i) #define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i) #define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i) #define random_shuffle(V) shuffle(V, rng) #define sim template<class n sim, class s> ostream & operator << (ostream &p, pair<n, s> x) {return p << "<" << x.f << ", " << x.s << ">";} sim> auto operator << (ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type {int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} sim, class s> void mini(n &p, s y) {if(p>y) p = y;} sim, class s> void maxi(n &p, s y) {if(p<y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define I(x) #x " =", (x), " " #define A(a, i) #a "[" #i " =", i, "] =", a[i], " " const int MXN = 305; #define PLL pair<LL, LL> #define VPLL vector<PLL> struct result { vector<VPLL> V; // dla każdego k pary (wielkość aktualna, wynik) int siz; result(int sizz) : V(sizz+1), siz(sizz) {} }; inline LL get(PLL &x) { return x.f * x.f + x.s; } void append(VPLL &res, PLL x) { while(res.size() && ((res.back().f >= x.f && res.back().s >= x.s) || (get(x) <= get(res.back()) && x.f <= res.back().f))) res.pop_back(); if(res.size() && ((res.back().f <= x.f && res.back().s <= x.s) || (x.f >= res.back().f && get(x) >= get(res.back())))); else res.PB(x); // assert(res.size() < 300); } // void check(VPLL &V) // { // REP(i, (int)V.size()-1) // { // assert(V[i].f <= V[i+1].f); // assert(V[i].s >= V[i+1].s); // assert(get(V[i]) >= get(V[i+1])); // } // } void merge_vec(VPLL &a, VPLL &b) // skleja dwa posortowane wektory i zapiszuje wynik w a { VPLL res; int i = 0; int j = 0; while(i < (int)a.size() || j < (int)b.size()) { if(j == (int)b.size() || (i < (int)a.size() && a[i].f < b[j].f)) append(res, a[i++]); else append(res, b[j++]); } a = res; // check(a); } result merge(result &A, result &B) { result C(A.siz + B.siz); FOR(b, 1, B.siz) { LL res = 1e18; for(auto &bb: B.V[b]) { mini(res, get(bb)); } FOR(a, 1, A.siz) { VPLL pom; for(auto &aa: A.V[a]) { pom.PB(MP(aa.f, aa.s + res)); } merge_vec(C.V[a+b], pom); } FOR(a, 1, A.siz) { VPLL pom; for(auto &bb: B.V[b]) { for(auto &aa: A.V[a]) pom.PB(MP(aa.f + bb.f, aa.s + bb.s)); } // assert(pom.size() < 6000); sort(ALL(pom)); merge_vec(C.V[a+b-1], pom); } } return C; } VI V[MXN]; int in[MXN]; int cnt = 0; result dfs(int v, int par) { vector<result> results; for(auto i: V[v]) { if(i == par)continue; auto x = dfs(i, v); results.PB(x); } result act(1); act.V[1].PB(MP(in[v], 0)); REP(j, results.size()) { act = merge(act, results[j]); } return act; } void solve() { int n; cin >> n; FOR(i, 1, n)V[i].clear(); FOR(i, 1, n)cin >> in[i]; REP(i, n-1) { int a, b; cin >> a >> b; V[a].PB(b); V[b].PB(a); } auto res = dfs(1, 0); FOR(i, 1, n) { LL val = 1e18; for(auto j: res.V[i])mini(val, j.f * j.f + j.s); cout << val << " "; } cout << endl; } int main() { ios_base::sync_with_stdio(0); int z; cin >> z; while(z--)solve(); } |