#include <algorithm> #include <vector> #include <queue> #include <cstdio> #define ll long long const ll INF = 8001002003004005006L; using namespace std; struct EdgeAct { bool act; EdgeAct(): act(false) {} }; struct Edge { int to; EdgeAct* a; Edge(int to, EdgeAct* a): to(to), a(a) {} }; int n; int a[333]; vector<vector<int>> edges; vector<vector<Edge>> ne; queue<int> q; bool visited[333]; ll dfs() { ll sqSum = 0; for(int i=0;i<n;i++) { visited[i] = false; } for(int i=0;i<n;i++) { if(!visited[i]) { visited[i] = true; ll sum = 0; q.push(i); while(!q.empty()) { int v = q.front(); q.pop(); sum += a[v]; vector<Edge>& e = ne[v]; for(auto it = e.begin();it != e.end();it++) { if(it->a->act) { if(!visited[it->to]) { visited[it->to] = true; q.push(it->to); } } } } sqSum += sum * sum; } } return sqSum; } vector<ll> solve() { for(int i=0;i<n;i++) { vector<Edge> e; vector<int>& e1 = edges[i]; for(auto it = e1.begin();it != e1.end();it++) { EdgeAct* ea = 0; int u = *it; if(i < u) { ea = new EdgeAct(); } else { vector<Edge>& e2 = ne[u]; for(auto it2 = e2.begin();it2 != e2.end();it2++) { if(it2->to == i) { ea = it2->a; break; } } } e.push_back(Edge(u, ea)); } ne.push_back(e); } vector<EdgeAct*> rs; for(int from=0;from<n;from++) { vector<Edge>& e = ne[from]; for(auto edge = e.begin();edge != e.end();edge++) { if(from < edge->to) { rs.push_back(edge->a); } } } vector<ll> res(n, INF); ll u = 1LL << (n-1); for(ll i=0;i<u;i++) { for(int j=0;j<n-1;j++) { rs[j]->act = ((1LL << j) & i) > 0; } ll minSqSum = dfs(); int k = n-1- __builtin_popcountll(i); res[k] = min(res[k], minSqSum); } return res; } int main(int argc, char** argv) { int t; scanf("%d", &t); for(int i=0;i<t;i++) { ne.clear(); scanf("%d", &n); for(int j=0;j<n;j++) { scanf("%d", &(a[j])); } edges.clear(); for(int i=0;i<n;i++) { edges.push_back(vector<int>()); } for(int j=1;j<n;j++) { int from, to; scanf("%d%d", &from, &to); from--; to--; edges[from].push_back(to); edges[to].push_back(from); } vector<ll> res = solve(); for(auto it=res.begin();it != res.end();it++) { if(it != res.begin()) { printf(" "); } printf("%lld", *it); } 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 | #include <algorithm> #include <vector> #include <queue> #include <cstdio> #define ll long long const ll INF = 8001002003004005006L; using namespace std; struct EdgeAct { bool act; EdgeAct(): act(false) {} }; struct Edge { int to; EdgeAct* a; Edge(int to, EdgeAct* a): to(to), a(a) {} }; int n; int a[333]; vector<vector<int>> edges; vector<vector<Edge>> ne; queue<int> q; bool visited[333]; ll dfs() { ll sqSum = 0; for(int i=0;i<n;i++) { visited[i] = false; } for(int i=0;i<n;i++) { if(!visited[i]) { visited[i] = true; ll sum = 0; q.push(i); while(!q.empty()) { int v = q.front(); q.pop(); sum += a[v]; vector<Edge>& e = ne[v]; for(auto it = e.begin();it != e.end();it++) { if(it->a->act) { if(!visited[it->to]) { visited[it->to] = true; q.push(it->to); } } } } sqSum += sum * sum; } } return sqSum; } vector<ll> solve() { for(int i=0;i<n;i++) { vector<Edge> e; vector<int>& e1 = edges[i]; for(auto it = e1.begin();it != e1.end();it++) { EdgeAct* ea = 0; int u = *it; if(i < u) { ea = new EdgeAct(); } else { vector<Edge>& e2 = ne[u]; for(auto it2 = e2.begin();it2 != e2.end();it2++) { if(it2->to == i) { ea = it2->a; break; } } } e.push_back(Edge(u, ea)); } ne.push_back(e); } vector<EdgeAct*> rs; for(int from=0;from<n;from++) { vector<Edge>& e = ne[from]; for(auto edge = e.begin();edge != e.end();edge++) { if(from < edge->to) { rs.push_back(edge->a); } } } vector<ll> res(n, INF); ll u = 1LL << (n-1); for(ll i=0;i<u;i++) { for(int j=0;j<n-1;j++) { rs[j]->act = ((1LL << j) & i) > 0; } ll minSqSum = dfs(); int k = n-1- __builtin_popcountll(i); res[k] = min(res[k], minSqSum); } return res; } int main(int argc, char** argv) { int t; scanf("%d", &t); for(int i=0;i<t;i++) { ne.clear(); scanf("%d", &n); for(int j=0;j<n;j++) { scanf("%d", &(a[j])); } edges.clear(); for(int i=0;i<n;i++) { edges.push_back(vector<int>()); } for(int j=1;j<n;j++) { int from, to; scanf("%d%d", &from, &to); from--; to--; edges[from].push_back(to); edges[to].push_back(from); } vector<ll> res = solve(); for(auto it=res.begin();it != res.end();it++) { if(it != res.begin()) { printf(" "); } printf("%lld", *it); } printf("\n"); } return 0; } |