QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#875759 | #4815. Flower's Land | Mirasycle | WA | 0ms | 8308kb | C++14 | 1.9kb | 2025-01-30 12:21:15 | 2025-01-30 12:21:16 |
Judging History
answer
#include<bits/stdc++.h>
#define pb emplace_back
using namespace std;
const int maxn=4e4+10;
const int maxk=3e3+10;
void cmax(int &x,int y){ x=x>y?x:y; }
int n,k,a[maxn],sz[maxn],son[maxn];
int f[maxn][maxk],g[maxn][maxk];
int ans[maxn],cur[maxn],h[maxk];
int pre[maxn],res[maxk]; vector<int> G[maxn];
void dfs(int u,int fa){//卷积 f
cur[u]=0; sz[u]=1;
for(auto v:G[u]){
if(v==fa) continue; memcpy(g[v],f[u],sizeof(f[u]));
dfs(v,u); int up=min(cur[u]+sz[v],k);
for(int i=cur[u];i>=0;i--)
for(int j=min(sz[v],up-i);j>=1;j--)
cmax(f[u][i+j],f[u][i]+f[v][j]);
cur[u]=pre[v]=up; sz[u]+=sz[v];
}
cur[u]=min(cur[u]+1,k);
for(int i=sz[u];i>=1;i--) f[u][i]=f[u][i-1]+a[u];
}
void Dp(int u,int fa){//对于每个点卷积答案,然后先卷积 h,再得到 g
// cout<<"U"<<u<<endl;
// for(int i=1;i<=k;i++) cout<<g[u][i]<<" "<<i<<endl;
for(int i=1;i<=min(sz[u],k);i++)
cmax(ans[u],f[u][i]+g[u][k-i]);
// cout<<"end"<<endl;
int c=0; son[0]=0;
for(auto v:G[u])
if(v!=fa) son[++c]=v;
if(!c) return ;
memcpy(h,g[u],sizeof(g[u]));//g[v] 为前缀背包,h 为后缀背包
int P=sz[u];//前缀大小
for(int z=c;z>=1;z--){
int v=son[z],lim=min(k,n-sz[v]); P-=sz[v];
for(int i=0;i<=k;i++) res[i]=0;
for(int p=pre[v];p>=0;p--)
for(int q=min(lim-1-p,k);q>=max(k-sz[v]-p-1,0);q--)
cmax(res[p+q+1],a[u]+g[v][p]+h[q]);
memcpy(g[v],res,sizeof(res));
for(int i=0;i<=k;i++) res[i]=0;
for(int i=1;i<=sz[v];i++)//更新后缀背包
for(int j=max(k-P-i-1,0);i+j<=k;j++)
cmax(res[i+j],f[v][i]+h[j]);
memcpy(h,res,sizeof(res));
}
for(auto v:G[u])
if(v!=fa) Dp(v,u);
}
int main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n-1;i++){
int u,v; cin>>u>>v;
G[u].pb(v); G[v].pb(u);
}
dfs(1,0); Dp(1,0);
for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 4736kb
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: 6804kb
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: 8308kb
input:
1 1 20
output:
20
result:
ok 1 number(s): "20"
Test #4:
score: 0
Accepted
time: 0ms
memory: 6692kb
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: 0ms
memory: 4992kb
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 2373 2373 1854 1880 1853 1536 2185 1945 2508 1707 2039 1649
result:
wrong answer 7th numbers differ - expected: '1960', found: '1854'