QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#875669 | #4815. Flower's Land | Mirasycle | WA | 1ms | 6728kb | C++14 | 2.3kb | 2025-01-30 01:07:10 | 2025-01-30 01:07:10 |
Judging History
answer
#include<bits/stdc++.h>
#define pb emplace_back
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int> > vii;
const int maxn=4e4+10;
void cmax(int &x,int y){ x=x>y?x:y; }
int n,k,a[maxn],sz[maxn],son[maxn];
vi G[maxn],f[maxn],g[maxn]; vii h[2];
int ans[maxn],cur[maxn];
void dfs(int u,int fa){//初始化 f 数组
for(auto v:G[u]){
if(v==fa) continue;
dfs(v,u); sz[u]+=sz[v];
}
sz[u]++; f[u].resize(sz[u]+1,0);
}
void F(int u,int fa){//卷积 f
cur[u]=1; f[u][1]=a[u];
for(auto v:G[u]){
if(v==fa) continue;
F(v,u); int up=min(cur[u]+cur[v],k);
for(int i=cur[u];i>=1;i--)//从 1 开始,因为强制选择 u
for(int j=min(cur[v],up-i);j>=1;j--)
cmax(f[u][i+j],f[u][i]+f[v][j]);
cur[u]=up;
}
}
void Dp(int u,int fa){//对于每个点卷积答案,然后先卷积 h,再得到 g
int S=g[u].size();//统计答案
for(int i=max(k-S+1,1);i<=min(sz[u],k);i++)
cmax(ans[u],f[u][i]+g[u][k-i]);
int c=0; son[0]=0;
for(auto v:G[u])
if(v!=fa) son[++c]=v;
if(!c) return ;
h[0].clear(); h[1].clear();
h[0].resize(c+1); h[1].resize(c+2);
h[1][c+1].resize(1,0);
//calc h0
h[0][0]=g[u]; int suf=sz[u];
for(int z=1;z<=c;z++){
int v=son[z]; suf-=sz[v];
h[0][z].resize(min(suf,k)+1,0);
for(int i=0;i<=min(suf+sz[v],k);i++)
for(int j=0;j<=sz[v]&&i+j<=min(suf,k);j++)
cmax(h[0][z][i+j],h[0][z-1][i]+f[v][j]);
}
//calc h1
h[1][c]=f[son[c]]; cur[u]=min(k,sz[son[c]]);
for(int z=c-1;z>=1;z--){
int v=son[z],up=min(cur[u]+sz[v],k);
h[1][z].resize(up+1,0);
for(int i=0;i<=cur[u];i++)
for(int j=0;j<=sz[v]&&i+j<=up;j++)
cmax(h[1][z][i+j],h[1][z+1][i]+f[v][j]);
cur[u]=up;
}
for(int i=1;i<=c;i++){//卷积 g
int v=son[i],z=min(k,n-sz[v]);
g[v].resize(z+1,0);
for(int p=0;p<h[0][i-1].size();p++)
for(int q=max(k-sz[v]-p-1,0);q<h[1][i+1].size()&&p+q+1<=z;q++)
cmax(g[v][p+q+1],a[u]+h[0][i-1][p]+h[1][i+1][q]);
}
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); F(1,0); g[1].pb(0); Dp(1,0);
for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 6400kb
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: 1ms
memory: 6728kb
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: 6728kb
input:
1 1 20
output:
20
result:
ok 1 number(s): "20"
Test #4:
score: 0
Accepted
time: 1ms
memory: 6348kb
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: 0
Accepted
time: 1ms
memory: 6600kb
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 2373 2373 1854 1940 1853 1536 2508 1945 2508 1945 2039 1827
result:
ok 20 numbers
Test #6:
score: -100
Wrong Answer
time: 0ms
memory: 6528kb
input:
40 5 1105 1687 737 6321 7793 7325 3443 2983 6912 6304 4211 5325 7774 7857 5121 8331 9317 1042 8291 9698 7373 440 9788 7938 7191 5563 4554 596 9733 4920 5398 3642 844 5733 4048 4417 8279 3054 4596 3153 12 17 12 36 12 15 12 13 12 2 12 30 12 18 12 33 12 4 12 39 12 25 12 20 12 10 12 9 12 23 12 29 12 3 1...
output:
35649 36231 35281 40865 42337 41869 37987 37527 41456 40848 38755 43861 42318 42401 39665 42875 43861 35586 42835 43861 41917 34984 43861 7938 41735 40107 39098 35140 43861 39464 39942 38186 35388 40277 38592 38961 42823 37598 39140 37697
result:
wrong answer 24th numbers differ - expected: '42482', found: '7938'