QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#73580 | #4815. Flower's Land | Unique_Hanpi | WA | 0ms | 6600kb | C++14 | 2.1kb | 2023-01-26 11:18:59 | 2023-01-26 11:19:01 |
Judging History
answer
#include <bits/stdc++.h>
#define lowbit(x) (x & -x)
#define eb emplace_back
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
const int N = 40005;
const int Mod = 998244353;
int n, k, a[N], ans[N];
int siz[N], dfn[N], nfd[N];
int f1[N][3005], f2[N][3005];
bool vis[N];
vector<int> T[N];
void dfs_centre(int x, int fa, int sizz, int &c) {
nfd[dfn[x] = ++dfn[0]] = x;
siz[x] = 1;
int mx = 0;
for (auto son : T[x]) {
if (son == fa || vis[son]) continue;
dfs_centre(son, x, sizz, c);
siz[x] += siz[son];
mx = max(mx, siz[son]);
}
if (mx <= sizz / 2 && siz[x] >= sizz / 2) c = x;
}
inline void chkMax(int &a, int b) { if (a < b) a = b; }
void dp(int n) {
for (int i = 1; i <= n + 1; i++)
for (int j = 0; j <= k; j++)
f1[i][j] = f2[i][j] = -1e9;
f1[1][0] = 0;
for (int i = 1; i < n; i++)
for (int j = 0; j <= k; j++) {
if (j < k) chkMax(f1[i + 1][j + 1], f1[i][j] + a[nfd[i]]);
chkMax(f1[i + siz[nfd[i]]][j], f1[i][j]);
}
f2[n + 1][0] = 0;
for (int i = n; i; i--)
for (int j = 0; j <= k; j++) {
if (j) chkMax(f2[i][j], f2[i + 1][j - 1] + a[nfd[i]]);
chkMax(f2[i][j], f2[i + siz[nfd[i]]][j]);
}
for (int i = 1; i <= n; i++)
for (int j = 0; j <= k - 1; j++)
chkMax(ans[nfd[i]], a[nfd[i]] + f1[i][j] + f2[i + 1][k - 1 - j]);
}
void solve(int rt, int sizz) {
if (sizz < k) return;
dfn[0] = 0;
dfs_centre(rt, rt, sizz, rt);
dp(dfn[0]);
vis[rt] = 1;
for (auto son : T[rt]) {
if (vis[son]) continue;
solve(son, siz[son] < siz[rt] ? siz[son] : sizz - siz[rt]);
}
}
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);
T[u].pb(v), T[v].pb(u);
}
solve(1, n);
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 6600kb
input:
5 3 6 10 4 3 4 3 4 4 2 2 5 5 1
output:
20 20 0 0 20
result:
wrong answer 3rd numbers differ - expected: '17', found: '0'