QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#288665 | #4815. Flower's Land | xlao | WA | 1ms | 8896kb | C++14 | 1.9kb | 2023-12-23 11:03:19 | 2023-12-23 11:03:19 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb emplace_back
int read()
{
char c=getchar(); int res=0, f=1;
while(!isdigit(c)) {if(c=='-') f=-1; c=getchar();}
while(isdigit(c)) {res=(res<<3)+(res<<1)+(c^48); c=getchar();}
return res*f;
}
const int N=4e4+1, M=3001, inf=1<<30;
int n,K,a[N],f[N][M],g[N][M],h[M],G[M],ans[N];
vector<int> E[N];
int sz[N];
void dfs1(int u,int fa)
{
for(int i=0;i<=K;++i) f[u][i]=-inf;
sz[u]=1, f[u][1]=a[u];
for(int v:E[u])
{
if(v==fa) continue;
dfs1(v,u);
for(int i=0;i<=K;++i) g[v][i]=f[u][i], h[i]=-inf;
for(int i=0; i<=min(K,sz[u]); ++i)
for(int j=0; i+j<=K && j<=sz[v]; ++j)
h[i+j] = max(h[i+j], f[u][i]+f[v][j]);
for(int i=0;i<=K;++i) f[u][i]=h[i];
sz[u]+=sz[v];
} f[u][0]=0;
// printf("On %d:\n",u);
// for(int i=0;i<=K;++i) printf("%d ",f[u][i]); puts("!");
}
void dfs2(int u,int fa)
{
g[u][K]=0;
for(int i=1;i<=K;++i) ans[u] = max(ans[u], f[u][i]+g[u][i]);
for(int i=0;i<=K;++i) G[i]=g[u][i];
// printf("On %d:\n",u);
// for(int i=K;i>=0;--i) printf("%d ",g[u][i]); puts("!");
reverse(E[u].begin(), E[u].end());
int pre=sz[u];
for(int v:E[u])
{
if(v==fa) continue;
pre-=sz[v];
for(int i=0;i<=K;++i) h[i]=-inf;
for(int i=0; i<=min(K,sz[v]); ++i)
for(int j=0; i+j<=K && j<=pre; ++j)
h[i] = max(h[i], g[v][j]+G[i+j]);
for(int i=0;i<=K;++i) g[v][i]=h[i], h[i]=-inf;
for(int i=0; i<=min(K,pre); ++i)
for(int j=0; i+j<=K && j<=sz[v]; ++j)
h[i] = max(h[i], f[v][j]+G[i+j]);
for(int i=0;i<=K;++i) G[i]=h[i];
dfs2(v,u);
}
}
int main()
{
n=read(), K=read();
for(int i=1;i<=n;++i) a[i]=read();
for(int i=1,x,y;i<n;++i) x=read(), y=read(), E[x].pb(y), E[y].pb(x);
dfs1(1,0);
for(int i=0;i<K;++i) g[1][i]=-inf;
dfs2(1,0);
for(int i=1;i<=n;++i) printf("%d ",ans[i]);
}
/*
5 3
6 10 4 3 4
3 4
4 2
2 5
5 1
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 8088kb
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: 0ms
memory: 8804kb
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: 6848kb
input:
1 1 20
output:
20
result:
ok 1 number(s): "20"
Test #4:
score: 0
Accepted
time: 1ms
memory: 6864kb
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: 1ms
memory: 8896kb
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 1854 1707 1960 2373 1854 0 1853 1536 2185 1945 2508 0 2039 0
result:
wrong answer 7th numbers differ - expected: '1960', found: '1854'