// Artur Kraska, II UWr #include <bits/stdc++.h> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(auto iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define ff first #define ss second #define deb(X) X; #define M 1000000007 #define INF 1000000000000000007LL using namespace std; int n, a, b, numer, kpoz; int zla[1007], bzla[1007], zla1[1007], kon[2][1007]; long long res[1007]; int usuniety; vector <int> worder, korder; struct krawedz { int id; int nr; }; krawedz kr; struct wierzch { vector <krawedz> l; int byl; long long waga; }; wierzch tab[1007]; long long sqr(long long x) { return x*x; } void wywal_krawedz() { //random_shuffle(korder.begin(), korder.end()); while(zla[kpoz]) kpoz = (kpoz+1)%(n-1); //if(zla[i] == 0) { zla[kpoz] = 1; usuniety = kon[0][kpoz]; kpoz = (kpoz+1)%(n-1); return ; } } long long policz_sume(int nr, int o) { long long res = tab[nr].waga; tab[nr].byl = numer; for(auto el : tab[nr].l) { if(el.nr != o && !zla[el.id]) res += policz_sume(el.nr, nr); } return res; } void balansuj(int nieuzywany) { bool ok = 0; int ile = 0; random_shuffle(worder.begin(), worder.end()); while(!ok) { ok = 1; //cerr << " iteracja " << ++ile << endl; for(auto nr : worder) //FORD(nr, n, 1) //FOR(nr, 1, n) { random_shuffle(tab[nr].l.begin(), tab[nr].l.end()); for(auto el1 : tab[nr].l) if(!zla[el1.id]) for(auto el2 : tab[nr].l) if(zla[el2.id]) { long long przed = sqr(policz_sume(nr, -1)) + sqr(policz_sume(el2.nr, -1)); swap(zla[el1.id], zla[el2.id]); long long po = sqr(policz_sume(nr, -1)) + sqr(policz_sume(el1.nr, -1)); if(po < przed) ok = 0; else swap(zla[el1.id], zla[el2.id]); } } } } int ok = 0; void balansuj_rek(int nr, int o) { random_shuffle(tab[nr].l.begin(), tab[nr].l.end()); for(auto el1 : tab[nr].l) if(!zla[el1.id]) for(auto el2 : tab[nr].l) if(zla[el2.id]) { long long przed = sqr(policz_sume(nr, -1)) + sqr(policz_sume(el2.nr, -1)); swap(zla[el1.id], zla[el2.id]); long long po = sqr(policz_sume(nr, -1)) + sqr(policz_sume(el1.nr, -1)); if(po < przed) ok = 0; else swap(zla[el1.id], zla[el2.id]); } for(auto el : tab[nr].l) if(el.nr != o) balansuj_rek(el.nr, nr); random_shuffle(tab[nr].l.begin(), tab[nr].l.end()); for(auto el1 : tab[nr].l) if(!zla[el1.id]) for(auto el2 : tab[nr].l) if(zla[el2.id]) { long long przed = sqr(policz_sume(nr, -1)) + sqr(policz_sume(el2.nr, -1)); swap(zla[el1.id], zla[el2.id]); long long po = sqr(policz_sume(nr, -1)) + sqr(policz_sume(el1.nr, -1)); if(po < przed) ok = 0; else swap(zla[el1.id], zla[el2.id]); } } void balansuj2(int nr) { ok = 0; int ile = 0; while(!ok) { ok = 1; balansuj_rek(nr, -1); } } void spychaj(int nr, int id) { for(auto el : tab[nr].l) if(el.id != id) { spychaj(el.nr, el.id); } if(id != -1 && zla[id]) { for(auto el : tab[nr].l) if(!zla[el.id]) { swap(zla[id], zla[el.id]); break; } } } long long licz_wynik() { long long res = 0; numer++; FOR(i, 1, n) { if(tab[i].byl != numer) res += sqr(policz_sume(i, -1)); } return res; } void orderuj(int nr, int o) { for(auto el : tab[nr].l) { if(el.nr != o) { orderuj(el.nr, nr); korder.PB(el.id); } } worder.PB(nr); } void porownaj(long long &var, long long val) { if(val < var) { var = val; forr(i, n-1) bzla[i] = zla[i]; } } int main() { testy() { worder.clear(); korder.clear(); long long pom = 0; scanf("%d", &n); FOR(i, 1, n) { scanf("%lld", &tab[i].waga); pom += tab[i].waga*tab[i].waga; tab[i].l.clear(); worder.PB(i); } //cout << pom << endl; forr(i, n-1) { scanf("%d %d", &a, &b); kr.id = i; zla[i] = 0; kon[0][i] = a; kon[1][i] = b; kr.nr = a; tab[b].l.PB(kr); kr.nr = b; tab[a].l.PB(kr); korder.PB(i); } orderuj(1, -1); printf("%lld", licz_wynik()); FOR(i, 1, n-1) { forr(i, n-1) zla1[i] = zla[i]; res[i] = INF; random_shuffle(korder.begin(), korder.end()); kpoz = 0; forr(_z, 8000/n) { forr(i, n-1) zla[i] = zla1[i]; wywal_krawedz(); balansuj2(usuniety); porownaj(res[i], licz_wynik()); FOR(x, 1, 0) { forr(_x, x) spychaj(usuniety, -1); balansuj2(usuniety); porownaj(res[i], licz_wynik()); } /* FOR(x, 1, 3) { forr(_x, x) spychaj(rand()%n+1, -1); balansuj(); porownaj(res[i], licz_wynik()); }*/ } printf(" %lld", res[i]); forr(i, n-1) { zla[i] = bzla[i]; } } printf("\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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | // Artur Kraska, II UWr #include <bits/stdc++.h> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(auto iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define ff first #define ss second #define deb(X) X; #define M 1000000007 #define INF 1000000000000000007LL using namespace std; int n, a, b, numer, kpoz; int zla[1007], bzla[1007], zla1[1007], kon[2][1007]; long long res[1007]; int usuniety; vector <int> worder, korder; struct krawedz { int id; int nr; }; krawedz kr; struct wierzch { vector <krawedz> l; int byl; long long waga; }; wierzch tab[1007]; long long sqr(long long x) { return x*x; } void wywal_krawedz() { //random_shuffle(korder.begin(), korder.end()); while(zla[kpoz]) kpoz = (kpoz+1)%(n-1); //if(zla[i] == 0) { zla[kpoz] = 1; usuniety = kon[0][kpoz]; kpoz = (kpoz+1)%(n-1); return ; } } long long policz_sume(int nr, int o) { long long res = tab[nr].waga; tab[nr].byl = numer; for(auto el : tab[nr].l) { if(el.nr != o && !zla[el.id]) res += policz_sume(el.nr, nr); } return res; } void balansuj(int nieuzywany) { bool ok = 0; int ile = 0; random_shuffle(worder.begin(), worder.end()); while(!ok) { ok = 1; //cerr << " iteracja " << ++ile << endl; for(auto nr : worder) //FORD(nr, n, 1) //FOR(nr, 1, n) { random_shuffle(tab[nr].l.begin(), tab[nr].l.end()); for(auto el1 : tab[nr].l) if(!zla[el1.id]) for(auto el2 : tab[nr].l) if(zla[el2.id]) { long long przed = sqr(policz_sume(nr, -1)) + sqr(policz_sume(el2.nr, -1)); swap(zla[el1.id], zla[el2.id]); long long po = sqr(policz_sume(nr, -1)) + sqr(policz_sume(el1.nr, -1)); if(po < przed) ok = 0; else swap(zla[el1.id], zla[el2.id]); } } } } int ok = 0; void balansuj_rek(int nr, int o) { random_shuffle(tab[nr].l.begin(), tab[nr].l.end()); for(auto el1 : tab[nr].l) if(!zla[el1.id]) for(auto el2 : tab[nr].l) if(zla[el2.id]) { long long przed = sqr(policz_sume(nr, -1)) + sqr(policz_sume(el2.nr, -1)); swap(zla[el1.id], zla[el2.id]); long long po = sqr(policz_sume(nr, -1)) + sqr(policz_sume(el1.nr, -1)); if(po < przed) ok = 0; else swap(zla[el1.id], zla[el2.id]); } for(auto el : tab[nr].l) if(el.nr != o) balansuj_rek(el.nr, nr); random_shuffle(tab[nr].l.begin(), tab[nr].l.end()); for(auto el1 : tab[nr].l) if(!zla[el1.id]) for(auto el2 : tab[nr].l) if(zla[el2.id]) { long long przed = sqr(policz_sume(nr, -1)) + sqr(policz_sume(el2.nr, -1)); swap(zla[el1.id], zla[el2.id]); long long po = sqr(policz_sume(nr, -1)) + sqr(policz_sume(el1.nr, -1)); if(po < przed) ok = 0; else swap(zla[el1.id], zla[el2.id]); } } void balansuj2(int nr) { ok = 0; int ile = 0; while(!ok) { ok = 1; balansuj_rek(nr, -1); } } void spychaj(int nr, int id) { for(auto el : tab[nr].l) if(el.id != id) { spychaj(el.nr, el.id); } if(id != -1 && zla[id]) { for(auto el : tab[nr].l) if(!zla[el.id]) { swap(zla[id], zla[el.id]); break; } } } long long licz_wynik() { long long res = 0; numer++; FOR(i, 1, n) { if(tab[i].byl != numer) res += sqr(policz_sume(i, -1)); } return res; } void orderuj(int nr, int o) { for(auto el : tab[nr].l) { if(el.nr != o) { orderuj(el.nr, nr); korder.PB(el.id); } } worder.PB(nr); } void porownaj(long long &var, long long val) { if(val < var) { var = val; forr(i, n-1) bzla[i] = zla[i]; } } int main() { testy() { worder.clear(); korder.clear(); long long pom = 0; scanf("%d", &n); FOR(i, 1, n) { scanf("%lld", &tab[i].waga); pom += tab[i].waga*tab[i].waga; tab[i].l.clear(); worder.PB(i); } //cout << pom << endl; forr(i, n-1) { scanf("%d %d", &a, &b); kr.id = i; zla[i] = 0; kon[0][i] = a; kon[1][i] = b; kr.nr = a; tab[b].l.PB(kr); kr.nr = b; tab[a].l.PB(kr); korder.PB(i); } orderuj(1, -1); printf("%lld", licz_wynik()); FOR(i, 1, n-1) { forr(i, n-1) zla1[i] = zla[i]; res[i] = INF; random_shuffle(korder.begin(), korder.end()); kpoz = 0; forr(_z, 8000/n) { forr(i, n-1) zla[i] = zla1[i]; wywal_krawedz(); balansuj2(usuniety); porownaj(res[i], licz_wynik()); FOR(x, 1, 0) { forr(_x, x) spychaj(usuniety, -1); balansuj2(usuniety); porownaj(res[i], licz_wynik()); } /* FOR(x, 1, 3) { forr(_x, x) spychaj(rand()%n+1, -1); balansuj(); porownaj(res[i], licz_wynik()); }*/ } printf(" %lld", res[i]); forr(i, n-1) { zla[i] = bzla[i]; } } printf("\n"); } return 0; } |