QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#72998 | #4815. Flower's Land | FISHER_ | WA | 1ms | 10136kb | C++14 | 1.8kb | 2023-01-21 16:07:45 | 2023-01-21 16:07:48 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int maxn = 40000, maxk = 3000;
int n, k;
int a[maxn + 5];
vector<int> g[maxn + 5];
bool vis[maxn + 5];
int siz[maxn + 5];
int ts, mn, rt;
void findRt(int u, int fa) {
siz[u] = 1;
int mx = 0;
for (int v : g[u]) {
if (v == fa || vis[v]) continue;
findRt(v, u);
siz[u] += siz[v], mx = max(mx, siz[v]);
}
mx = max(mx, ts - siz[u]);
if (mx < mn) mn = mx, rt = u;
}
int rnk[maxn + 5], stamp;
void dfs(int u, int fa) {
rnk[++stamp] = u, siz[u] = 1;
for (int v : g[u]) {
if (v == fa || vis[v]) continue;
dfs(v, u);
siz[u] += siz[v];
}
}
int f[maxn + 5][maxk + 5], b[maxn + 5][maxk + 5];
int ans[maxn + 5];
void divide(int u, int tot) {
if (tot < k) return;
ts = tot, mn = INT_MAX, findRt(u, 0);
vis[u = rt] = 1;
stamp = 0, dfs(u, 0);
for (int i = 1; i <= tot + 1; i++) memset(f[i], 192, sizeof(f[i])), memset(b[i], 192, sizeof(b[i]));
f[1][0] = 0;
for (int i = 1; i <= tot; i++)
for (int j = 0; j < k; j++) {
f[i + 1][j + 1] = max(f[i + 1][j + 1], f[i][j] + a[rnk[i]]);
f[i + siz[rnk[i]]][j] = max(f[i + siz[rnk[i]]][j], f[i][j]);
}
b[tot + 1][0] = 0;
for (int i = tot; i > 0; i--)
for (int j = 0; j <= k; j++) {
if (j) b[i][j] = max(b[i][j], b[i + 1][j - 1] + a[rnk[i]]);
b[i][j] = max(b[i][j], b[i + siz[rnk[i]]][j]);
}
for (int i = 1; i <= tot; i++)
for (int j = 0; j < k; j++) ans[rnk[i]] = max(ans[rnk[i]], f[i][j] + b[i][k - j]);
for (int v : g[u])
if (!vis[v]) divide(v, siz[v]);
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
g[u].push_back(v), g[v].push_back(u);
}
divide(1, n);
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 10136kb
input:
5 3 6 10 4 3 4 3 4 4 2 2 5 5 1
output:
20 20 17 20 20
result:
wrong answer 4th numbers differ - expected: '17', found: '20'