#include <cstdio>
#include <vector>
using namespace std;
#define FOR(i,a,b) for(int (i)=(int)(a); (i)!=(int)(b); ++(i))
struct FindUnionNode {
FindUnionNode *fuParent;
unsigned int fuRank, fuSize;
FindUnionNode() {
fuInit();
}
void fuInit() {
fuParent = 0;
fuRank = 0;
fuSize = 1;
}
FindUnionNode *fuFind() {
if (fuParent == 0) return this;
fuParent = fuParent->fuFind();
return fuParent;
}
void fuUnion(FindUnionNode *other) {
FindUnionNode *xRoot = this->fuFind();
FindUnionNode *yRoot = other->fuFind();
if (xRoot != yRoot) {
if (xRoot->fuRank > yRoot->fuRank) {
yRoot->fuParent = xRoot;
xRoot->fuSize += yRoot->fuSize;
} else if (xRoot->fuRank < yRoot->fuRank) {
xRoot->fuParent = yRoot;
yRoot->fuSize += xRoot->fuSize;
} else {
yRoot->fuParent = xRoot;
xRoot->fuSize += yRoot->fuSize;
++xRoot->fuRank;
}
}
}
};
struct City : public FindUnionNode {
int power;
long long sum;
City():power(0),sum(0){}
};
int z, n, a, b;
int main() {
scanf("%d", &z);
while (z--) {
scanf("%d", &n);
vector<City> A(n);
FOR(i,0,n) scanf("%d", &A[i].power);
vector<pair<int,int> > E(n-1);
FOR(i,0,n-1) {
scanf("%d %d", &a, &b);
E[i] = make_pair(a-1, b-1);
}
vector<long long int> R(n, 90000000000000000LL);
FOR(t,0,1<<(n-1)) {
FOR(i,0,n) { A[i].fuInit(); A[i].sum = 0; }
int tt = t, destroyed = 0;
FOR(i,0,n-1) {
if (tt % 2 == 1) {
A[E[i].first].fuUnion(&A[E[i].second]);
} else ++destroyed;
tt >>= 1;
}
FOR(i,0,n) {
City *parent = (City *)(A[i].fuFind());
parent->sum += (long long)(A[i].power);
}
long long tmp_sum = 0;
FOR(i,0,n) tmp_sum += A[i].sum * A[i].sum;
if (R[destroyed] == -1) R[destroyed] = tmp_sum; else R[destroyed] = min(R[destroyed], tmp_sum);
}
FOR(i,0,n) printf("%lld ", R[i]);
printf("\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 | #include <cstdio> #include <vector> using namespace std; #define FOR(i,a,b) for(int (i)=(int)(a); (i)!=(int)(b); ++(i)) struct FindUnionNode { FindUnionNode *fuParent; unsigned int fuRank, fuSize; FindUnionNode() { fuInit(); } void fuInit() { fuParent = 0; fuRank = 0; fuSize = 1; } FindUnionNode *fuFind() { if (fuParent == 0) return this; fuParent = fuParent->fuFind(); return fuParent; } void fuUnion(FindUnionNode *other) { FindUnionNode *xRoot = this->fuFind(); FindUnionNode *yRoot = other->fuFind(); if (xRoot != yRoot) { if (xRoot->fuRank > yRoot->fuRank) { yRoot->fuParent = xRoot; xRoot->fuSize += yRoot->fuSize; } else if (xRoot->fuRank < yRoot->fuRank) { xRoot->fuParent = yRoot; yRoot->fuSize += xRoot->fuSize; } else { yRoot->fuParent = xRoot; xRoot->fuSize += yRoot->fuSize; ++xRoot->fuRank; } } } }; struct City : public FindUnionNode { int power; long long sum; City():power(0),sum(0){} }; int z, n, a, b; int main() { scanf("%d", &z); while (z--) { scanf("%d", &n); vector<City> A(n); FOR(i,0,n) scanf("%d", &A[i].power); vector<pair<int,int> > E(n-1); FOR(i,0,n-1) { scanf("%d %d", &a, &b); E[i] = make_pair(a-1, b-1); } vector<long long int> R(n, 90000000000000000LL); FOR(t,0,1<<(n-1)) { FOR(i,0,n) { A[i].fuInit(); A[i].sum = 0; } int tt = t, destroyed = 0; FOR(i,0,n-1) { if (tt % 2 == 1) { A[E[i].first].fuUnion(&A[E[i].second]); } else ++destroyed; tt >>= 1; } FOR(i,0,n) { City *parent = (City *)(A[i].fuFind()); parent->sum += (long long)(A[i].power); } long long tmp_sum = 0; FOR(i,0,n) tmp_sum += A[i].sum * A[i].sum; if (R[destroyed] == -1) R[destroyed] = tmp_sum; else R[destroyed] = min(R[destroyed], tmp_sum); } FOR(i,0,n) printf("%lld ", R[i]); printf("\n"); } } |
English