QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#72493#4815. Flower's LandxuqinWA 1ms5712kbC++142.4kb2023-01-15 21:48:422023-01-15 21:48:43

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-15 21:48:43]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5712kb
  • [2023-01-15 21:48:42]
  • 提交

answer

#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;
const int maxn=40020, maxk=3020;

struct pj{int y, nxt;} G[maxn<<1];
int len, la[maxn], a[maxn], n, k, v[maxn], tot, sz[maxn], rt, mx[maxn], dep[maxn], sum[maxn];
int pre[maxn], back[maxn], cnt, cnt1, f[maxn][maxk], g[maxn][maxk], now[maxn], ans[maxn], pos[maxn];

void add_e(int x, int y) {
	G[++len].y=y; G[len].nxt=la[x]; la[x]=len;
}

void fdrt(int x, int F) {
	sz[x]=1; mx[x]=0;
	for(int i=la[x]; i; i=G[i].nxt) {
		int y=G[i].y;
		if(y==F||v[y]) continue;
		fdrt(y, x);
		sz[x]+=sz[y];
		mx[x]=max(mx[x], sz[y]);
	}
	mx[x]=max(mx[x], tot-sz[x]);
	if(rt==0||mx[x]<mx[rt]) rt=x;
}

void dfs(int x, int F) {
	pre[++cnt]=x; dep[x]=dep[F]+1; sum[x]=sum[F]+a[x]; sz[x]=1;
	for(int i=la[x]; i; i=G[i].nxt) {
		int y=G[i].y;
		if(y==F||v[y]) continue;
		dfs(y, x); sz[x]+=sz[y];
	}
	back[++cnt1]=x;
}

void upd(int x, int F) {
	for(int i=la[x]; i; i=G[i].nxt) {
		int y=G[i].y;
		if(y==F||v[y]) continue;
		upd(y, x);
		now[x]=max(now[x], now[y]);
	}
}
void gettot(int x, int F) {
	++tot;
	for(int i=la[x]; i; i=G[i].nxt) {
		int y=G[i].y;
		if(y==F||v[y]) continue;
		gettot(y, x);
	}
}

void sol(int x) {
	cnt=cnt1=0; dfs(x, 0);
	if(sz[x]<k) return;
	reverse(back+1, back+cnt+1);
	for(int i=1; i<=cnt; ++i) pos[back[i]]=i;
	for(int i=1; i<=cnt; ++i)
		for(int j=0; j<=i&&j<=k; ++j) f[i][j]=g[i][j]=-2e9;
	f[1][0]=g[1][0]=0;
	for(int i=1; i<=cnt; ++i)
		for(int j=0; j<=i&&j<=k; ++j) {
			f[i+1][j+1]=max(f[i+1][j+1], f[i][j]+a[pre[i]]),
			f[i+sz[pre[i]]][j]=max(f[i+sz[pre[i]]][j], f[i][j]), 
			g[i+1][j+1]=max(g[i+1][j+1], g[i][j]+a[back[i]]),
			g[i+sz[back[i]]][j]=max(g[i+sz[back[i]]][j], g[i][j]);
		}
	for(int i=1; i<=cnt; ++i) {
		int xx=pre[i];
		now[xx]=0;
		for(int j=0; j<=k-dep[xx]; ++j) 
			now[xx]=max(now[xx], f[i][j+dep[xx]-1]+g[pos[pre[i]]][k-j-1]-sum[xx]+2*a[xx]);
	}
	
	upd(x, 0);
	for(int i=1; i<=cnt; ++i) ans[pre[i]]=max(ans[pre[i]], now[pre[i]]);
	v[x]=1;
	for(int i=la[x]; i; i=G[i].nxt) {
		int y=G[i].y;
		if(v[y]) continue;
		tot=0; gettot(y, 0);
		rt=0; fdrt(y, 0);
		sol(rt);
	}
}

int main() {
	scanf("%d%d", &n, &k);
	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_e(x, y), add_e(y, x);
	tot=n; fdrt(1, 0);
	sol(rt);
	for(int i=1; i<=n; ++i) printf("%d ", ans[i]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 5712kb

input:

5 3
6 10 4 3 4
3 4
4 2
2 5
5 1

output:

20 294967306 17 17 20 

result:

wrong answer 2nd numbers differ - expected: '20', found: '294967306'