QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#72080#4815. Flower's LandchenshiRE 3ms7720kbC++1.4kb2023-01-13 23:34:102023-01-13 23:34:13

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-13 23:34:13]
  • 评测
  • 测评结果:RE
  • 用时:3ms
  • 内存:7720kb
  • [2023-01-13 23:34:10]
  • 提交

answer

#include<cstdio>
#include<iostream>
using namespace std;
const int o=40010;const long long inf=1e18;
int n,K,a[o],G,seq[o],s[o],hs[o],h[o],cnt;long long f[o][3010],g[o][3010],ans[o];bool vis[o];
struct Edge{int v,p;}e[o*2];
inline void ad(int U,int V){e[++cnt].v=V;e[cnt].p=h[U];h[U]=cnt;}
void dfs(int nw,int fa){
	s[seq[++cnt]=nw]=1;hs[nw]=0;
	for(int i=h[nw];i;i=e[i].p) if(e[i].v-fa&&!vis[e[i].v])
		dfs(e[i].v,nw),s[nw]+=s[e[i].v],hs[nw]=(s[hs[nw]]>s[e[i].v]?hs[nw]:e[i].v);
}
void dvd(int nw){
	dfs(G=nw,cnt=0);
	for(int i=1;i<=cnt;++i) if(max(s[nw]-s[seq[i]],s[hs[i]])<max(s[nw]-s[G],s[hs[G]])) G=seq[i];
	dfs(G,cnt=0);vis[G]=1;
	for(int i=0;i<=cnt+1;++i) for(int j=0;j<K;++j) f[i][j]=g[i][j]=-inf;
	f[0][0]=g[cnt+1][0]=0;
	for(int i=1;i<=cnt;++i){
		for(int j=1;j<K;++j) f[i][j]=max(f[i][j],f[i-1][j-1]+a[seq[i]]);
		for(int j=0,t=i+s[seq[i]]-1;j<K;++j) f[t][j]=max(f[t][j],f[i-1][j]);
	}
	for(int i=cnt;i;--i){
		for(int j=1;j<K;++j) g[i][j]=max(g[i][j],g[i+1][j-1]+a[seq[i]]);
		for(int j=0,t=i+s[seq[i]];j<K;++j) g[i][j]=max(g[i][j],g[t][j]);
	}
	for(int i=1;i<=cnt;++i) for(int j=0;j<K;++j)
		ans[seq[i]]=max(ans[seq[i]],f[i-1][j]+g[i+1][K-j-1]+a[seq[i]]);
	for(int i=h[G];i;i=e[i].p) if(s[e[i].v]>=K) dvd(e[i].v);
}
int main(){
	scanf("%d%d",&n,&K);
	for(int i=1;i<=n;++i) scanf("%d",&a[i]);
	for(int i=1,u,v;i<n;++i) scanf("%d%d",&u,&v),ad(u,v),ad(v,u);
	dvd(1);
	for(int i=1;i<=n;++i) printf("%lld ",ans[i]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 5728kb

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: 7720kb

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: 7696kb

input:

1 1
20

output:

20 

result:

ok 1 number(s): "20"

Test #4:

score: 0
Accepted
time: 3ms
memory: 5708kb

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
Runtime Error

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:


result: