QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#76414#4815. Flower's Land275307894aTL 3779ms1884252kbC++142.2kb2023-02-09 20:32:402023-02-09 20:32:41

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-09 20:32:41]
  • 评测
  • 测评结果:TL
  • 用时:3779ms
  • 内存:1884252kb
  • [2023-02-09 20:32:40]
  • 提交

answer

#include<bits/stdc++.h>
#define Gc() getchar() 
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define PB push_back
using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned ll;
using namespace std;const int N=4e4+5,M=3e3+5,K=2e3+5,mod=998244353,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(time(0));
int n,k,x,y,A[N],f[N],Rt,Ct,vis[N],f1[N*2][M],f2[N*2][M],Df[N*2],Dh,Si[N],B1[N],B2[N],E1[N],E2[N],Ans[N];vector<int> S[N];
void GI(int x,int La){Si[x]=1;for(int i:S[x]) i^La&&!vis[i]&&(GI(i,x),Si[x]+=Si[i]);}
void DP(int x,int La){f[x]=Ct-Si[x];for(int i:S[x]) i^La&&!vis[i]&&(DP(i,x),f[x]=max(f[x],Si[i]));f[x]<f[Rt]&&(Rt=x);}
void Make(int x,int La,int *Bg,int *En){Df[Bg[x]=++Dh]=x;Si[x]=1;for(int i:S[x]) i^La&&!vis[i]&&(Make(i,x,Bg,En),Si[x]+=Si[i]);Df[En[x]=++Dh]=0;reverse(S[x].begin(),S[x].end());}
void ckx(int &x,int y){x<y&&(x=y);} 
void Qry(int x,int La,int w,int Sum){
	int p1=B1[x],p2=E2[x]-1;Sum+=A[x];
	for(int i=w;i<=k;i++) Ans[x]=max(Ans[x],f1[p1][i]+f2[p2][k-i+w]-Sum);
	for(int i:S[x]) i^La&&!vis[i]&&(Qry(i,x,w+1,Sum),0);
}
void dfs(int x){
	GI(x,0);Ct=Si[x];if(Ct<k) return;f[Rt=0]=INF;DP(x,0);x=Rt;vis[x]=1;int i,j;
	Dh=0;Make(x,0,B1,E1);for(i=1;i<=Dh;i++)Me(f1,0);
	f1[1][1]=A[Df[1]];for(i=1;i<Dh;i++){
		if(Df[i+1]){
			for(j=1;j<=k;j++) if(f1[i][j]) ckx(f1[E1[Df[i+1]]][j],f1[i][j]);
			for(j=1;j<k;j++) if(f1[i][j]) ckx(f1[i+1][j+1],f1[i][j]+A[Df[i+1]]);
		} 
		else for(j=1;j<=k;j++) ckx(f1[i+1][j],f1[i][j]);
	}
	Dh=0;Make(x,0,B2,E2);for(i=1;i<=Dh;i++) Me(f2,0);
	f2[1][1]=A[Df[1]];for(i=1;i<Dh;i++){
		if(Df[i+1]){
			for(j=1;j<=k;j++) if(f2[i][j]) ckx(f2[E2[Df[i+1]]][j],f2[i][j]);
			for(j=1;j<k;j++) if(f2[i][j]) ckx(f2[i+1][j+1],f2[i][j]+A[Df[i+1]]);
		} 
		else for(j=1;j<=k;j++) ckx(f2[i+1][j],f2[i][j]);
	}
	Qry(x,0,1,0);
	for(int i:S[x]) !vis[i]&&(dfs(i),0);
}
int main(){
	int i,j;scanf("%d%d",&n,&k);for(i=1;i<=n;i++) scanf("%d",&A[i]);for(i=1;i<n;i++) scanf("%d%d",&x,&y),S[x].PB(y),S[y].PB(x);
	dfs(1);for(i=1;i<=n;i++) printf("%d ",Ans[i]);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2865ms
memory: 1884252kb

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: 3779ms
memory: 1884248kb

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: 578ms
memory: 1884240kb

input:

1 1
20

output:

20 

result:

ok 1 number(s): "20"

Test #4:

score: -100
Time Limit Exceeded

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: