QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#511179 | #4815. Flower's Land | peter | WA | 52ms | 944312kb | C++14 | 2.1kb | 2024-08-09 17:10:36 | 2024-08-09 17:10:37 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=4e4+5;
const int maxk=3e3+5;
int dp[maxn][maxk],f[maxn][maxk];
struct edge{
int to,nxt;
}e[maxn<<1];
int head[maxn],len,n,k,a[maxn],rt,tot,res[maxn];
int sz[maxn],maxx[maxn],dfx[maxn],tim=0;
bool bk[maxn];
inline void init(){
memset(head,-1,sizeof(head));
memset(dp,-0x3f,sizeof(dp));
memset(f,-0x3f,sizeof(f));
memset(res,-0x3f,sizeof(res));
len=0;
maxx[0]=inf;
}
void add(int u,int v){
e[++len].to=v;
e[len].nxt=head[u];
head[u]=len;
}
void dfs(int u,int f){
dfx[++tim]=u;
sz[u]=1;
for(int i=head[u];i!=-1;i=e[i].nxt){
int v=e[i].to;
if(v==f||bk[v]) continue;
dfs(v,u);
sz[u]+=sz[v];
}
}
void getrt(int u,int f){
sz[u]=1;
for(int i=head[u];i!=-1;i=e[i].nxt){
int v=e[i].to;
if(v==f||bk[v]) continue;
getrt(v,u);
sz[u]+=sz[v];
maxx[u]=max(maxx[u],sz[v]);
}
maxx[u]=max(maxx[u],tot-sz[u]);
if(maxx[rt]>maxx[u]) rt=u;
}
void solve(int u){
bk[u]=1;
tim=0;
dfs(u,0);
dp[1][0]=f[sz[u]+1][0]=0;
for(int i=1;i<sz[u];i++){
for(int j=0;j<k;j++) dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+a[dfx[i]]);
if(i!=1){
for(int j=0;j<=k;j++) dp[i+sz[dfx[i]]][j]=max(dp[i+sz[dfx[i]]][j],dp[i][j]);
}
}
for(int i=sz[u];i>=1;i--){
for(int j=1;j<=k;j++) f[i][j]=f[i+1][j-1]+a[dfx[i]];
for(int j=0;j<=k;j++) res[dfx[i]]=max(res[dfx[i]],dp[i][j]+f[i][k-j]);
if(i!=1){
for(int j=0;j<=k;j++) f[i][j]=max(f[i][j],f[i+sz[dfx[i]]][j]);
}
}
f[sz[u]+1][0]=dp[1][0]=-inf;
for(int i=1;i<=sz[u];i++){
for(int j=0;j<=k;j++) dp[i][j]=f[i][j]=-inf;
}
int lst=tot;
for(int i=head[u];i!=-1;i=e[i].nxt){
int v=e[i].to;
if(bk[v]) continue;
if(sz[v]>sz[u]) tot=lst-sz[u];
else tot=sz[v];
if(tot<k) continue;
rt=0;
getrt(v,u);
solve(rt);
}
}
int main(){
init();
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);
add(u,v);
add(v,u);
}
getrt(1,0);
solve(rt);
for(int i=1;i<=n;i++) printf("%d ",res[i]);
puts("");
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 44ms
memory: 944312kb
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: 19ms
memory: 943316kb
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: 52ms
memory: 943408kb
input:
1 1 20
output:
20
result:
ok 1 number(s): "20"
Test #4:
score: 0
Accepted
time: 19ms
memory: 943376kb
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: 32ms
memory: 944068kb
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 2576 1945 2576 1945 2039 1827
result:
wrong answer 15th numbers differ - expected: '2508', found: '2576'