QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#73810 | #4815. Flower's Land | 12345678 | WA | 271ms | 1882996kb | C++14 | 2.2kb | 2023-01-28 23:14:08 | 2023-01-28 23:14:09 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define inf 1e9
#define pii pair <int, int>
const int mod = 1e9 + 7;
inline int read () {
int x = 0, f = 1;
char ch = getchar ();
while (ch < '0' || ch > '9') f = ((ch == '-') ? -1 : f), ch = getchar ();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar ();
return x * f;
}
inline void write (int x) {
if (x < 0) x = -x, putchar ('-');
if (x >= 10) write (x / 10);
putchar (x % 10 + '0');
}
int n, k;
int siz[40005], a[40005], f[40005][3005], g[40005][3005];
int ans[40005], tmp[3005], tmp2[3005];
vector <int> G[40005];
void dfs1(int x, int fa) {
siz[x] = 1;
f[x][1] = a[x];
for(auto y : G[x]) {
if(y == fa) continue;
dfs1(y, x);
memcpy(g[y], f[x], sizeof f[x]);
memset(tmp, -0x3f, sizeof tmp);
for(int i = 0; i <= min(k, siz[x]); i++) for(int j = 0; j <= min(k - i, siz[y]); j++) tmp[i+j] = max(tmp[i+j], f[x][i] + f[y][j]);
memcpy(f[x], tmp, sizeof tmp);
siz[x] += siz[y];
}
f[x][0] = 0;
}
void dfs2(int x, int fa) {
int Siz = siz[x];
g[x][k] = 0;
for(int i = 0; i <= k; i++) ans[x] = max(ans[x], f[x][i] + g[x][i]);
reverse(G[x].begin(), G[x].end());
memcpy(tmp2, g[x], sizeof tmp2);
for(auto y : G[x]) {
if(y == fa) continue;
Siz -= siz[y];
memset(tmp, -0x3f, sizeof tmp);
for(int i = 0; i <= min(k, siz[y]); i++) for(int j = 0; j <= min(k - i, Siz); j++) tmp[i] = max(tmp[i], tmp2[i+j] + g[y][j]);
memcpy(g[y], tmp, sizeof tmp);
memset(tmp, -0x3f, sizeof tmp);
for(int i = 0; i <= min(k, Siz); i++) for(int j = 0; j <= min(k - i, siz[y]); j++) tmp[i] = max(tmp[i], tmp2[i+j] + f[y][j]);
memcpy(tmp2, tmp, sizeof tmp);
}
for(auto y : G[x]) if(y != fa) dfs2(y, x);
}
signed main () {
// freopen (".in", "r", stdin);
// freopen (".out", "w", stdout);
n = read(), k = read();
for(int i = 1; i <= n; i++) a[i] = read();
for(int i = 1; i < n; i++) {
int u = read(), v = read();
G[u].push_back(v);
G[v].push_back(u);
}
memset(f, -0x3f, sizeof f), memset(g, -0x3f, sizeof g), memset(ans, -0x3f, sizeof ans);
dfs1(1, 0), dfs2(1, 0);
for(int i = 1; i <= n; i++) write(ans[i]), putchar(' ');
putchar('\n');
return 0;
}
/*
*/
詳細信息
Test #1:
score: 0
Wrong Answer
time: 271ms
memory: 1882996kb
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'