QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#72215 | #4815. Flower's Land | zhoukangyang | WA | 64ms | 944584kb | C++17 | 1.6kb | 2023-01-15 09:20:14 | 2023-01-15 09:20:16 |
Judging History
answer
#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define ll long long
#define sz(a) ((int) (a).size())
#define vi vector < int >
#define me(a, x) memset(a, x, sizeof(a))
#define ull unsigned long long
#define ld __float128
using namespace std;
const int N = 40007, M = 3007;
int n, k, a[N], ns[N];
int f[N][M], A[M], B[M], T[M], g[N][M], siz[N];
vi e[N];
void dfs1(int x, int fa) {
siz[x] = 1, f[x][1] = a[x];
for(auto v : e[x]) if(v != fa) {
dfs1(v, x);
L(i, 0, k) g[v][i] = f[x][i];
me(T, -0x3f);
L(i, 0, min(k, siz[x])) L(j, 0, min(k - i, siz[v])) T[i + j] = max(T[i + j], f[x][i] + f[v][j]);
L(i, 0, k) f[x][i] = T[i];
siz[x] += siz[v];
}
f[x][0] = 0;
}
void dfs2(int x, int fa) {
int sz = siz[x];
g[x][k] = 0;
L(i, 0, k) A[i] = g[x][i];
// A = mutiply(A, f_x)
// B = B * f_x + A_i * g_x
for(auto v : e[x]) if(v != fa) {
sz -= siz[v];
me(T, -0x3f);
L(i, 0, min(k, siz[v])) L(j, 0, min(k - i, sz)) T[i] = max(T[i], A[i + j] + g[v][j]);
L(i, 0, k) g[v][i] = T[i];
me(T, -0x3f);
L(i, 0, min(k, sz)) L(j, 0, min(k - i, siz[v])) T[i] = max(T[i], A[i + j] + f[v][j]);
L(i, 0, k) A[i] = T[i];
}
L(i, 1, k) ns[x] = max(ns[x], f[x][i] + g[x][i]);
for(auto v : e[x]) if(v != fa) dfs2(v, x);
}
int main () {
ios :: sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> k;
L(i, 1, n) cin >> a[i];
L(i, 1, n - 1) {
int u, v;
cin >> u >> v;
e[u].emplace_back(v);
e[v].emplace_back(u);
}
me(f, -0x3f), me(g, -0x3f);
dfs1(1, 0);
dfs2(1, 0);
L(i, 1, n) cout << ns[i] << ' ';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 44ms
memory: 944356kb
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: 56ms
memory: 944356kb
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: 56ms
memory: 944584kb
input:
1 1 20
output:
20
result:
ok 1 number(s): "20"
Test #4:
score: 0
Accepted
time: 64ms
memory: 944372kb
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 180 169 94 180 137 137 169 159 180
result:
ok 10 numbers
Test #5:
score: -100
Wrong Answer
time: 52ms
memory: 944364kb
input:
20 3 932 609 248 720 831 253 418 482 1000 542 436 304 217 163 872 380 704 845 497 610 17 12 1 17 15 17 13 17 2 15 16 2 18 16 8 16 4 16 19 4 6 4 20 19 10 19 9 10 5 10 7 9 3 9 14 5 11 7
output:
2508 2185 1790 1945 2373 1470 1960 1707 2039 2373 1854 1940 1853 1536 2508 1945 2508 1834 2039 1827
result:
wrong answer 9th numbers differ - expected: '2373', found: '2039'