QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#95665#4815. Flower's Landcrazy_seaWA 3ms9612kbC++111.5kb2023-04-11 11:12:552023-04-11 11:12:57

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-11 11:12:57]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:9612kb
  • [2023-04-11 11:12:55]
  • 提交

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'