QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#504262 | #4815. Flower's Land | caijianhong | WA | 1ms | 6756kb | C++23 | 2.4kb | 2024-08-04 11:07:31 | 2024-08-04 11:07:32 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define endl "\n"
#define debug(...) void(0)
#endif
using LL = long long;
int n, k, a[40010], siz[40010], rnk[40010], cnt;
basic_string<int> g[40010];
bool cut[40010];
int findcen(int rt, int T) {/*{{{*/
pair<int, int> cen{(int)1e9, 0};
auto dfs = [&](auto& dfs, int u, int fa) -> void {
int smx = siz[u] = 1;
for (int v : g[u]) if (!cut[v] && v != fa) dfs(dfs, v, u), siz[u] += siz[v], smx = max(smx, siz[v]);
cen = min(cen, {max(smx, T - siz[u]), u});
};
return dfs(dfs, rt, 0), cen.second;
}/*}}}*/
int fans[40010];
void dfs(int kd, int u, int fa) {
rnk[++cnt] = u;
siz[u] = 1;
if (kd) reverse(g[u].begin(), g[u].end());
for (int v : g[u]) if (v != fa && !cut[v]) dfs(kd, v, u), siz[u] += siz[v];
if (kd) reverse(g[u].begin(), g[u].end());
}
vector<int> f[40010], tmp[40010][2];
void calc(int u, int fa, int sum, int c) {
if (++c > k) return ;
sum += a[u];
const auto& [lhs, rhs] = tmp[u];
#ifdef LOCAL
debug("calc(%d, sum=%d, c=%d)\n", u, sum, c);
debug("lhs: "); for (int x : lhs) debug("%d, ", x); debug("\n");
debug("rhs: "); for (int x : rhs) debug("%d, ", x); debug("\n");
#endif
for (int i = 0; i <= k - c && i < (int)lhs.size(); i++) {
if (k - c - i < (int)rhs.size()) fans[u] = max(fans[u], sum + lhs[i] + rhs[k - c - i]);
}
for (int v : g[u]) if (v != fa && !cut[v]) calc(v, u, sum, c);
}
void work(int rt) {
for (int kd : {0, 1}) {
dfs(kd, rt, cnt = 0);
if (cnt < k) return ;
f[cnt + 1] = {0};
for (int j = cnt; j >= 1; j--) {
f[j] = f[j + siz[rnk[j]]];
if (f[j].size() < f[j + 1].size() + 1) f[j].resize(f[j + 1].size() + 1, numeric_limits<int>::min());
for (size_t k = 0; k < f[j + 1].size(); k++) f[j][k + 1] = max(f[j][k + 1], f[j + 1][k] + a[rnk[j]]);
}
for (int j = cnt; j >= 1; j--) tmp[rnk[j]][kd] = move(f[j + 1]);
}
debug("rt = %d\n", rt);
calc(rt, 0, 0, 0);
}
void solve(int rt) {
cut[rt] = true;
work(rt);
for (int v : g[rt]) if (!cut[v]) solve(findcen(v, siz[v]));
}
int main() {
#ifndef LOCAL
cin.tie(nullptr)->sync_with_stdio(false);
#endif
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1, u, v; i < n; i++) cin >> u >> v, g[u] += v, g[v] += u;
solve(findcen(1, n));
for (int i = 1; i <= n; i++) cout << fans[i] << " \n"[i == n];
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 6292kb
input:
5 3 6 10 4 3 4 3 4 4 2 2 5 5 1
output:
20 20 17 17 20
result:
ok 5 number(s): "20 20 17 17 20"
Test #2:
score: 0
Accepted
time: 1ms
memory: 6756kb
input:
7 4 1 3 2 1 7 12 17 4 6 1 4 5 1 2 5 7 6 3 2
output:
31 13 13 31 21 31 31
result:
ok 7 numbers
Test #3:
score: 0
Accepted
time: 0ms
memory: 6492kb
input:
1 1 20
output:
20
result:
ok 1 number(s): "20"
Test #4:
score: -100
Wrong Answer
time: 1ms
memory: 6100kb
input:
10 3 19 7 25 18 93 97 21 51 60 80 6 7 7 1 1 9 9 10 10 2 2 5 5 3 3 8 8 4
output:
159 193 211 94 180 137 137 169 159 180
result:
wrong answer 2nd numbers differ - expected: '180', found: '193'