QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#95665 | #4815. Flower's Land | crazy_sea | WA | 3ms | 9612kb | C++11 | 1.5kb | 2023-04-11 11:12:55 | 2023-04-11 11:12:57 |
Judging History
answer
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int N=4e4+10,K=3e3+10,inf=1e9;
struct edge{
int next,to;
}e[N<<1];
int first[N],len,siz[N],a[N],f[N][K],g[N][K],h[N],fa[N],n,m,ans[N];
void add(int a,int b)
{
e[++len]=edge{first[a],b};
first[a]=len;
}
vector<int> to[N];
void dfs1(int x)
{
for(int i=0;i<=m;i++) f[x][i]=g[x][i]=-inf;
siz[x]=1,f[x][1]=a[x];
for(int i=first[x],y;i;i=e[i].next)
if((y=e[i].to)!=fa[x])
{
to[x].push_back(y),fa[y]=x,dfs1(y);
for(int j=0;j<=m;j++) g[y][j]=f[x][j],f[x][j]=-inf;
for(int j=0;j<=min(m,siz[y]);j++)
for(int k=0;k<=min(m-j,siz[x]);k++)
f[x][j+k]=max(f[x][j+k],f[y][j]+g[y][k]);
siz[x]+=siz[y],g[y][m]=0;
}
f[x][0]=g[x][m]=0;
}
void dfs2(int x)
{
int s=siz[x],y;
for(int i=1;i<=m;i++) ans[x]=max(ans[x],f[x][i]+g[x][i]);
for(int i=(int)to[x].size()-1;i>=0;i--)
{
y=to[x][i],s-=siz[y];
for(int j=0;j<=m;j++) h[j]=-inf;
for(int j=0;j<=min(m,s);j++)
for(int k=0;k<=min(m-j,siz[y]);k++)
h[k]=max(h[k],g[x][j+k]+g[y][j]);
for(int j=0;j<=m;j++) g[y][j]=h[j],h[j]=-inf;
for(int j=0;j<=min(m,siz[y]);j++)
for(int k=0;k<=min(m-j,s);k++)
h[k]=max(h[k],g[x][j+k]+f[y][j]);
for(int j=0;j<=m;j++) g[x][j]=h[j];
}
for(int y:to[x]) dfs2(y);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1,x,y;i<n;i++)
scanf("%d%d",&x,&y),add(x,y),add(y,x);
dfs1(1),dfs2(1);
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: 3ms
memory: 9612kb
input:
5 3 6 10 4 3 4 3 4 4 2 2 5 5 1
output:
20 20 0 0 20
result:
wrong answer 3rd numbers differ - expected: '17', found: '0'