#include <bits/stdc++.h> using namespace std; inline int nextInt() { int n; scanf("%d", &n); return n; } vector<vector<int> > graph; int deg[300]; struct Item { int k; int s; long long s2; bool operator<(const Item &oth) { if (k != oth.k) return k < oth.k; if (s != oth.s) return s < oth.s; return s2 < oth.s2; } }; typedef vector<Item> Data; Data vData[300]; int value[300]; void addEdge(int a, int b) { graph[a].push_back(b); graph[b].push_back(a); } void readGraph(int n) { graph = vector<vector<int> >(n, vector<int>()); for (int i=0; i<n; ++i) { vData[i] = Data(1, {0, 0, 0} ); } for (int i=0; i<n; ++i) value[i] = nextInt(); for (int i=1; i<n; ++i) { int a = nextInt() - 1; int b = nextInt() - 1; addEdge(a, b); } } void makeGraph(int n) { // initGraph(n); map<int, int> l1; for (int i=0; i<n-2; ++i) l1[i] = rand() % n; set<int> l2; for (int i=0; i<n; ++i) l2.insert(i); int c = 0; while (!l1.empty()) { set<int> l1val; for (map<int, int>::iterator it = l1.begin(); it != l1.end(); ++it) l1val.insert(it->second); int i; for (set<int>::iterator it = l2.begin(); it != l2.end(); ++it) { if (l1val.count(*it) > 0) continue; i = *it; break; } addEdge(i, l1[c]); l1.erase(c); l2.erase(i); ++c; } int v1 = *l2.begin(); l2.erase(l2.begin()); int v2 = *l2.begin(); addEdge(v1, v2); } void solve() { int n = nextInt(); readGraph(n); queue<int> q; for (int i=0; i<n; ++i) { deg[i] = graph[i].size(); if (deg[i] == 1) { q.push(i); } // fprintf(stderr, "v with deg 1: %d\n", i + 1); } int finish; while (!q.empty()) { int v = q.front(); q.pop(); if (graph[v].empty()) { // fprintf(stderr, "finish vertex %d\n", v + 1); finish = v; break; } int vv = graph[v][0]; for (int i=0; i<graph[vv].size(); ++i) if (graph[vv][i] == v) { if (i < graph[vv].size() - 1) { graph[vv][i] = graph[vv].back(); } graph[vv].pop_back(); } if (graph[vv].size() == 1) q.push(vv); // fprintf(stderr, "process %d => %d\n", v + 1, vv + 1); // for (int i=0; i<vData[v].size(); ++i) // fprintf(stderr, " vertex %d item %d %d %lld\n", v+1, vData[v][i].k, vData[v][i].s, vData[v][i].s2); // for (int i=0; i<vData[vv].size(); ++i) // fprintf(stderr, " vertex %d item %d %d %lld\n", vv+1, vData[vv][i].k, vData[vv][i].s, vData[vv][i].s2); Data newData; Data &d1 = vData[v]; Data &d2 = vData[vv]; for (int i=0; i<d1.size(); ++i) { for (int j=0; j<d2.size(); ++j) { newData.push_back({ d1[i].k + d2[j].k, d1[i].s + d2[j].s + value[v], d1[i].s2 + d2[j].s2 }); newData.push_back({ d1[i].k + d2[j].k + 1, d2[j].s, d1[i].s2 + d2[j].s2 + 1LL * (d1[i].s + value[v]) * (d1[i].s + value[v]) }); } } sort(newData.begin(), newData.end()); Data newData2; newData2.push_back(newData[0]); for (int i=1; i<newData.size(); ++i) { if (newData2.back().k == newData[i].k) { long long L = newData2.back().s2 - newData[i].s2; L += 1LL * newData2.back().s * newData2.back().s; L -= 1LL * newData[i].s * newData[i].s; long long R = 2LL * value[vv] * (newData[i].s - newData2.back().s); if (L <= R) { // fprintf(stderr, " skip %d %d %lld => %d %d %lld with value %d\n", // newData2.back().k, // newData2.back().s, // newData2.back().s2, // newData[i].k, // newData[i].s, // newData[i].s2, // value[vv] // ); continue; } } newData2.push_back(newData[i]); } swap(newData2, d2); // for (int i=0; i<vData[vv].size(); ++i) // fprintf(stderr, " new vertex %d item %d %d %lld\n", vv+1, vData[vv][i].k, vData[vv][i].s, vData[vv][i].s2); } vector<long long> res(n, 1000000000000000000LL); for (int i=0; i<vData[finish].size(); ++i) { int k = vData[finish][i].k; int s = vData[finish][i].s; long long s2 = vData[finish][i].s2; long long cur = s2 + 1LL * (s + value[finish]) * (s + value[finish]); if (cur < res[k]) res[k] = cur; } for (int i=0; i<n; ++i) printf("%lld%s", res[i], i+1==n ? "\n" : " "); } int main() { int TC = nextInt(); for (int tc=0; tc<TC; ++tc) solve(); 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 | #include <bits/stdc++.h> using namespace std; inline int nextInt() { int n; scanf("%d", &n); return n; } vector<vector<int> > graph; int deg[300]; struct Item { int k; int s; long long s2; bool operator<(const Item &oth) { if (k != oth.k) return k < oth.k; if (s != oth.s) return s < oth.s; return s2 < oth.s2; } }; typedef vector<Item> Data; Data vData[300]; int value[300]; void addEdge(int a, int b) { graph[a].push_back(b); graph[b].push_back(a); } void readGraph(int n) { graph = vector<vector<int> >(n, vector<int>()); for (int i=0; i<n; ++i) { vData[i] = Data(1, {0, 0, 0} ); } for (int i=0; i<n; ++i) value[i] = nextInt(); for (int i=1; i<n; ++i) { int a = nextInt() - 1; int b = nextInt() - 1; addEdge(a, b); } } void makeGraph(int n) { // initGraph(n); map<int, int> l1; for (int i=0; i<n-2; ++i) l1[i] = rand() % n; set<int> l2; for (int i=0; i<n; ++i) l2.insert(i); int c = 0; while (!l1.empty()) { set<int> l1val; for (map<int, int>::iterator it = l1.begin(); it != l1.end(); ++it) l1val.insert(it->second); int i; for (set<int>::iterator it = l2.begin(); it != l2.end(); ++it) { if (l1val.count(*it) > 0) continue; i = *it; break; } addEdge(i, l1[c]); l1.erase(c); l2.erase(i); ++c; } int v1 = *l2.begin(); l2.erase(l2.begin()); int v2 = *l2.begin(); addEdge(v1, v2); } void solve() { int n = nextInt(); readGraph(n); queue<int> q; for (int i=0; i<n; ++i) { deg[i] = graph[i].size(); if (deg[i] == 1) { q.push(i); } // fprintf(stderr, "v with deg 1: %d\n", i + 1); } int finish; while (!q.empty()) { int v = q.front(); q.pop(); if (graph[v].empty()) { // fprintf(stderr, "finish vertex %d\n", v + 1); finish = v; break; } int vv = graph[v][0]; for (int i=0; i<graph[vv].size(); ++i) if (graph[vv][i] == v) { if (i < graph[vv].size() - 1) { graph[vv][i] = graph[vv].back(); } graph[vv].pop_back(); } if (graph[vv].size() == 1) q.push(vv); // fprintf(stderr, "process %d => %d\n", v + 1, vv + 1); // for (int i=0; i<vData[v].size(); ++i) // fprintf(stderr, " vertex %d item %d %d %lld\n", v+1, vData[v][i].k, vData[v][i].s, vData[v][i].s2); // for (int i=0; i<vData[vv].size(); ++i) // fprintf(stderr, " vertex %d item %d %d %lld\n", vv+1, vData[vv][i].k, vData[vv][i].s, vData[vv][i].s2); Data newData; Data &d1 = vData[v]; Data &d2 = vData[vv]; for (int i=0; i<d1.size(); ++i) { for (int j=0; j<d2.size(); ++j) { newData.push_back({ d1[i].k + d2[j].k, d1[i].s + d2[j].s + value[v], d1[i].s2 + d2[j].s2 }); newData.push_back({ d1[i].k + d2[j].k + 1, d2[j].s, d1[i].s2 + d2[j].s2 + 1LL * (d1[i].s + value[v]) * (d1[i].s + value[v]) }); } } sort(newData.begin(), newData.end()); Data newData2; newData2.push_back(newData[0]); for (int i=1; i<newData.size(); ++i) { if (newData2.back().k == newData[i].k) { long long L = newData2.back().s2 - newData[i].s2; L += 1LL * newData2.back().s * newData2.back().s; L -= 1LL * newData[i].s * newData[i].s; long long R = 2LL * value[vv] * (newData[i].s - newData2.back().s); if (L <= R) { // fprintf(stderr, " skip %d %d %lld => %d %d %lld with value %d\n", // newData2.back().k, // newData2.back().s, // newData2.back().s2, // newData[i].k, // newData[i].s, // newData[i].s2, // value[vv] // ); continue; } } newData2.push_back(newData[i]); } swap(newData2, d2); // for (int i=0; i<vData[vv].size(); ++i) // fprintf(stderr, " new vertex %d item %d %d %lld\n", vv+1, vData[vv][i].k, vData[vv][i].s, vData[vv][i].s2); } vector<long long> res(n, 1000000000000000000LL); for (int i=0; i<vData[finish].size(); ++i) { int k = vData[finish][i].k; int s = vData[finish][i].s; long long s2 = vData[finish][i].s2; long long cur = s2 + 1LL * (s + value[finish]) * (s + value[finish]); if (cur < res[k]) res[k] = cur; } for (int i=0; i<n; ++i) printf("%lld%s", res[i], i+1==n ? "\n" : " "); } int main() { int TC = nextInt(); for (int tc=0; tc<TC; ++tc) solve(); return 0; } |