#include <cstdio> #include <vector> #include <queue> const long long inf = 1000000000000000000LL; const int max_n = 301; int n; int a[max_n]; std::vector<int> T[max_n]; std::vector<int> R[max_n]; long long sums[max_n]; long long DP[max_n][max_n]; long long LP[max_n][max_n]; std::deque<int> candidates; int counter; int checks; void brut(const int& v, int gl, long long sum, long long sumc) { ++counter; if (!candidates.empty()) { int u = candidates.front(); candidates.pop_front(); sumc -= sums[u]; for (int i = 0; i <= n; ++i) LP[gl + 1][i] = inf; for (int k = 1; k <= n - gl; ++k) { if (DP[u][k] < inf) { for (int j = gl; j <= n - k; ++j) { if (LP[gl][j] < inf) { LP[gl + 1][j + k] = std::min(LP[gl + 1][j + k], LP[gl][j] + DP[u][k]); } else break; } } else break; } bool good = false; for (int i = 0; i < n; ++i) { if (DP[v][i + 1] > LP[gl + 1][i] + sum * sum + sumc) { good = true; break; } } if (/*true || */good) brut(v, gl + 1, sum, sumc); for (int i = 0; i < R[u].size(); ++i) { candidates.push_back(R[u][i]); sumc += sums[R[u][i]]; } good = false; for (int i = 0; i < n; ++i) { if (DP[v][i + 1] > LP[gl][i] + (sum + a[u]) * (sum + a[u]) + sumc) { good = true; break; } } if (/*true || */good) brut(v, gl, sum + a[u], sumc); for (int i = 0; i < R[u].size(); ++i) { candidates.pop_back(); } candidates.push_front(u); } else { ++checks; for (int i = 0; i < n; ++i) { DP[v][i + 1] = std::min(DP[v][i + 1], LP[gl][i] + sum * sum); } } } void evaluate(int v) { // printf("evaluate: %d\n", v); long long sum = a[v], sumc = 0; candidates.clear(); for (int i = 0; i < R[v].size(); ++i) { candidates.push_back(R[v][i]); sumc += sums[R[v][i]]; } for (int i = 0; i <= n; ++i) { DP[v][i] = inf; LP[0][i] = inf; } LP[0][0] = 0; counter = checks = 0; brut(v, 0, sum, sumc); // printf("evaluate: %d %d %d\n", v, counter, checks); // for (int i = 0; i <= n; ++i) { // printf ("[%d][%d] = %lld\n", v, i, DP[v][i]); // } } int dfs(int v, int u) { int ret = 0; for (int i = 0; i < T[v].size(); ++i) { if (T[v][i] != u) { ret = std::max(ret, dfs(T[v][i], v)); // ret += dfs(T[v][i], v); } } return ret + 1; } void root(int v, int u) { // printf("root: %d %d\n", v, u); sums[v] = 0; for (int i = 0; i < T[v].size(); ++i) { if (T[v][i] != u) { R[v].push_back(T[v][i]); root(T[v][i], v); sums[v] += sums[T[v][i]]; } } sums[v] += ((long long)(a[v])) * a[v]; evaluate(v); } void gogo() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", a + i); for (int i = 0; i < n; ++i) { T[i].clear(); R[i].clear(); } for (int i = 1; i < n; ++i) { int u, v; scanf("%d %d", &u, &v); --u, --v; T[u].push_back(v); T[v].push_back(u); } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { DP[i][j] = -1; } } int best = n + 1; int vv = 0; for (int i = 0; i < n; ++i) { int local = 0; for (int j = 0; j < T[i].size(); ++j) { local = std::max(local, dfs(T[i][j], i)); } if (local < best) { best = local; vv = i; } } // printf ("vv=%d %d\n", vv, best); root(vv, vv); for (int i = 1; i <= n; ++i) printf("%lld ", DP[vv][i]); printf("\n"); } int main() { int tests; scanf("%d", &tests); while(tests--) gogo(); }
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 | #include <cstdio> #include <vector> #include <queue> const long long inf = 1000000000000000000LL; const int max_n = 301; int n; int a[max_n]; std::vector<int> T[max_n]; std::vector<int> R[max_n]; long long sums[max_n]; long long DP[max_n][max_n]; long long LP[max_n][max_n]; std::deque<int> candidates; int counter; int checks; void brut(const int& v, int gl, long long sum, long long sumc) { ++counter; if (!candidates.empty()) { int u = candidates.front(); candidates.pop_front(); sumc -= sums[u]; for (int i = 0; i <= n; ++i) LP[gl + 1][i] = inf; for (int k = 1; k <= n - gl; ++k) { if (DP[u][k] < inf) { for (int j = gl; j <= n - k; ++j) { if (LP[gl][j] < inf) { LP[gl + 1][j + k] = std::min(LP[gl + 1][j + k], LP[gl][j] + DP[u][k]); } else break; } } else break; } bool good = false; for (int i = 0; i < n; ++i) { if (DP[v][i + 1] > LP[gl + 1][i] + sum * sum + sumc) { good = true; break; } } if (/*true || */good) brut(v, gl + 1, sum, sumc); for (int i = 0; i < R[u].size(); ++i) { candidates.push_back(R[u][i]); sumc += sums[R[u][i]]; } good = false; for (int i = 0; i < n; ++i) { if (DP[v][i + 1] > LP[gl][i] + (sum + a[u]) * (sum + a[u]) + sumc) { good = true; break; } } if (/*true || */good) brut(v, gl, sum + a[u], sumc); for (int i = 0; i < R[u].size(); ++i) { candidates.pop_back(); } candidates.push_front(u); } else { ++checks; for (int i = 0; i < n; ++i) { DP[v][i + 1] = std::min(DP[v][i + 1], LP[gl][i] + sum * sum); } } } void evaluate(int v) { // printf("evaluate: %d\n", v); long long sum = a[v], sumc = 0; candidates.clear(); for (int i = 0; i < R[v].size(); ++i) { candidates.push_back(R[v][i]); sumc += sums[R[v][i]]; } for (int i = 0; i <= n; ++i) { DP[v][i] = inf; LP[0][i] = inf; } LP[0][0] = 0; counter = checks = 0; brut(v, 0, sum, sumc); // printf("evaluate: %d %d %d\n", v, counter, checks); // for (int i = 0; i <= n; ++i) { // printf ("[%d][%d] = %lld\n", v, i, DP[v][i]); // } } int dfs(int v, int u) { int ret = 0; for (int i = 0; i < T[v].size(); ++i) { if (T[v][i] != u) { ret = std::max(ret, dfs(T[v][i], v)); // ret += dfs(T[v][i], v); } } return ret + 1; } void root(int v, int u) { // printf("root: %d %d\n", v, u); sums[v] = 0; for (int i = 0; i < T[v].size(); ++i) { if (T[v][i] != u) { R[v].push_back(T[v][i]); root(T[v][i], v); sums[v] += sums[T[v][i]]; } } sums[v] += ((long long)(a[v])) * a[v]; evaluate(v); } void gogo() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", a + i); for (int i = 0; i < n; ++i) { T[i].clear(); R[i].clear(); } for (int i = 1; i < n; ++i) { int u, v; scanf("%d %d", &u, &v); --u, --v; T[u].push_back(v); T[v].push_back(u); } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { DP[i][j] = -1; } } int best = n + 1; int vv = 0; for (int i = 0; i < n; ++i) { int local = 0; for (int j = 0; j < T[i].size(); ++j) { local = std::max(local, dfs(T[i][j], i)); } if (local < best) { best = local; vv = i; } } // printf ("vv=%d %d\n", vv, best); root(vv, vv); for (int i = 1; i <= n; ++i) printf("%lld ", DP[vv][i]); printf("\n"); } int main() { int tests; scanf("%d", &tests); while(tests--) gogo(); } |