QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#265715 | #4815. Flower's Land | _Du_ | WA | 0ms | 8052kb | C++20 | 1.8kb | 2023-11-25 20:36:26 | 2023-11-25 20:36:27 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=40005,K=3005,INF=1e9;
int n,k,hd[N],mn,f[N][K],g[N][K],a[N],sz[N],dfn[N],ans[N],rt,res,vs[N],e_num,idx,ret;
struct edge{
int v,nxt;
}e[N<<1];
void add_edge(int u,int v)
{
e[++e_num]=(edge){v,hd[u]};
hd[u]=e_num;
}
void dfs(int x,int y)
{
sz[x]=1,dfn[++idx]=x;;
for(int i=hd[x];i;i=e[i].nxt)
if(e[i].v^y&&!vs[e[i].v])
dfs(e[i].v,x),sz[x]+=sz[e[i].v];
}
void getsz(int x,int y,int n)
{
int ret=0;
sz[x]=1;
for(int i=hd[x];i;i=e[i].nxt)
if(e[i].v^y&&!vs[e[i].v])
getsz(e[i].v,x,n),ret=max(ret,sz[e[i].v]),sz[x]+=sz[e[i].v];
ret=max(ret,n-sz[x]);
if(ret<mn)
mn=ret,rt=x;
}
int findrt(int x,int n)
{
mn=INF;
getsz(x,0,n);
return rt;
}
void solve(int x)
{
dfs(x,idx=0);
res+=sz[x]*k;
for(int i=1;i<=sz[x]+1;i++)
memset(f[i],-0x3f,sizeof(f[i])),memset(g[i],-0x3f,sizeof(g[i]));
for(int i=0;i<=k;i++)
f[1][i]=g[sz[x]+1][i]=0;
for(int i=1;i<=sz[x];i++)
{
for(int j=0;j<k;j++)
f[i+1][j+1]=max(f[i+1][j+1],f[i][j]+a[dfn[i]]);
if(i^1)
for(int j=0;j<=k;j++)
f[i+sz[dfn[i]]][j]=max(f[i][j],f[i+sz[dfn[i]]][j]);
}
for(int i=sz[x];i;i--)
{
for(int j=1;j<=k;j++)
g[i][j]=g[i+1][j-1]+a[dfn[i]];
if(i^1)
{
for(int j=0;j<=k;j++)
g[i][j]=max(g[i][j],g[i+sz[dfn[i]]][j]);
}
for(int j=0;j<=k;j++)
ans[dfn[i]]=max(ans[dfn[i]],f[i][j]+g[i][k-j]);
}
vs[x]=1;
for(int i=hd[x];i;i=e[i].nxt)
if(sz[e[i].v]>=k&&!vs[e[i].v])
solve(findrt(e[i].v,sz[e[i].v]));
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d",a+i);
for(int u,v,i=1;i<n;i++)
scanf("%d%d",&u,&v),add_edge(u,v),add_edge(v,u);
solve(findrt(1,n));
for(int i=1;i<=n;i++)
printf("%d ",ans[i]);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 8052kb
input:
5 3 6 10 4 3 4 3 4 4 2 2 5 5 1
output:
20 20 17 20 20
result:
wrong answer 4th numbers differ - expected: '17', found: '20'