QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#72563#4815. Flower's LandyyyyxhRE 2ms5652kbC++142.1kb2023-01-16 17:20:372023-01-16 17:20:39

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-16 17:20:39]
  • 评测
  • 测评结果:RE
  • 用时:2ms
  • 内存:5652kb
  • [2023-01-16 17:20:37]
  • 提交

answer

#include <cstdio>
using namespace std;
int read(){
	char c=getchar();int x=0;
	while(c<48||c>57) c=getchar();
	do x=(x<<1)+(x<<3)+(c^48),c=getchar();
	while(c>=48&&c<=57);
	return x;
}
const int N=40003,K=3003,INF=0x3f3f3f3f;
int n,k;
int hd[N],ver[N<<1],nxt[N<<1],tot;
void add(int u,int v){
	nxt[++tot]=hd[u];hd[u]=tot;ver[tot]=v;
}
int val[N],res[N];
void chmx(int &x,int v){if(x<v) x=v;}
bool del[N];
int ad[N],as[N],anum;
int bd[N],bs[N],bnum;
int sz[N],sn[N];
void dfs(int u,int fa){
	sz[as[ad[u]=++anum]=u]=1;
	for(int i=hd[u];i;i=nxt[i]){
		int v=ver[i];
		if(v==fa||del[v]) continue;
		dfs(v,u);sz[u]+=sz[v];
	}
	bs[bd[u]=++bnum]=u;
}
void calc(int u,int fa){
	sz[u]=1;
	for(int i=hd[u];i;i=nxt[i]){
		int v=ver[i];
		if(v==fa||del[v]) continue;
		calc(v,u);
		sz[u]+=sz[v];
		if(sz[v]>sz[sn[u]]) sn[u]=v;
	}
}
int f[N][K],g[N][K];
void upd(int u,int fa,int cnt,int sum){
	++cnt;sum+=val[u];
	for(int i=0;i<=k-cnt;++i)
		chmx(res[u],f[ad[u]+sz[u]][i]+g[bd[u]-1][k-cnt-i]+sum);
	for(int i=hd[u];i;i=nxt[i]){
		int v=ver[i];
		if(v==fa||del[v]) continue;
		upd(v,u,cnt,sum);
	}
}
void proc(int u){
	calc(u,0);int m=sz[u];
	while(2*sz[sn[u]]>m) u=sn[u];
	del[u]=1;anum=bnum=0;dfs(u,0);
	g[0][0]=f[m+1][0]=0;
	for(int i=1;i<=k;++i) g[0][i]=f[m+1][i]=-INF;
	for(int i=m;i;--i)
		for(int j=0;j<=k;++j){
			int x=as[i];
			f[i][j]=f[i+sz[x]][j];
			if(j) chmx(f[i][j],f[i+1][j-1]+val[x]);
		}
	for(int i=1;i<=m;++i)
		for(int j=0;j<=k;++j){
			int x=bs[i];
			g[i][j]=g[i-sz[x]][j];
			if(j) chmx(g[i][j],g[i-1][j-1]+val[x]);
		}
	upd(u,0,0,0);
	for(int i=hd[u];i;i=nxt[i]){
		int v=ver[i];
		if(del[v]||sz[v]<k) continue;
		proc(v);
	}
}
int main(){
	n=read();k=read();
	for(int i=1;i<=n;++i) val[i]=read();
	for(int i=1;i<n;++i){int u=read(),v=read();add(u,v);add(v,u);}
	proc(1);
	for(int i=1;i<=n;++i) printf("%d ",res[i]);
	putchar('\n');
	return 0;
	anum=bnum=0;dfs(1,0);
	for(int x=1;x<=n;++x){
		for(int i=ad[x]+sz[x];i<=n;++i) printf("%d ",as[i]);
		for(int i=1;i<bd[x];++i) printf("%d ",bs[i]);
		putchar('\n');
	}
	return 0;
}
/*
7 4
1 3 2 1 7 12 17
4 6
1 4
5 1
2 5
7 6
3 2
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5612kb

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: 2ms
memory: 5652kb

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: 1ms
memory: 5604kb

input:

1 1
20

output:

20 

result:

ok 1 number(s): "20"

Test #4:

score: -100
Runtime Error

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:


result: