QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#72422#4815. Flower's LandMaxFlowWA 3ms10236kbC++142.1kb2023-01-15 15:30:292023-01-15 15:31:55

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 15:31:55]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:10236kb
  • [2023-01-15 15:30:29]
  • 提交

answer

#include <bits/stdc++.h>
#define ri register int
#define ll long long
#define fi first
#define se second
using namespace std;
const int Maxn=4e4;
const int Maxm=3e3;
const int Inf=2e9;
vector<int>adj[Maxn+5];
int a[Maxn+5];
int f[Maxn+5][Maxm+5],g[Maxn+5][Maxm+5];
int pre[Maxn+5],suf[Maxn+5],in_pre[Maxn+5],in_suf[Maxn+5],t_pre,t_suf;
int ans[Maxn+5];
int vis[Maxn+5],size[Maxn+5];
vector<pair<int,int>>s;
int n,k;
void dfs(int u,int fa,int n) {
	size[u]=1;
	int mx=0;
	for(auto v:adj[u]) {
		if(v!=fa&&!vis[v]) {
			dfs(v,u,n);
			size[u]+=size[v];
			mx=max(mx,size[v]);
		}
	}
	s.emplace_back(max(mx,n-size[u]),u);
}
void dfs1(int u,int fa) {
	size[u]=1;
	pre[++t_pre]=u,in_suf[u]=t_suf+1,in_pre[u]=t_pre;
	for(auto v:adj[u]) {
		if(v!=fa&&!vis[v]) {
			dfs1(v,u);
			size[u]+=size[v];
		}
	}
	suf[++t_suf]=u;
}
void dfs2(int u,int fa,int dep,int sum) {
	int need=k-dep;
	for(ri i=0;i<=need;i++) {
		ans[u]=max(ans[u],g[in_suf[u]-1][i]+f[in_pre[u]+1][need-i]+sum);
	}
	for(auto v:adj[u]) {
		if(v!=fa&&!vis[v]) {
			dfs2(v,u,dep+1,sum+a[v]);
		}
	}
}
void work(int u) {
	if((int)s.size()<k)return ;
	dfs1(u,0);
	for(ri i=1;i<=k;i++)f[n+1][i]=g[0][i]=-Inf;
	f[n+1][0]=0,g[0][0]=0;
	for(ri i=n;i>=1;i--) {
		int u=pre[i];
		for(ri j=0;j<=k;j++) {
			f[i][j]=f[i+size[u]][j];
			if(j) {
				f[i][j]=max(f[i][j],f[i+1][j-1]+a[u]);
			}
		}
	}
	for(ri i=1;i<=n;i++) {
		int u=suf[i];
		for(ri j=0;j<=k;j++) {
			g[i][j]=g[i-size[u]][j];
			if(j) {
				g[i][j]=max(g[i][j],g[i-1][j-1]+a[u]);
			}
		}
	}
	dfs2(u,0,1,a[u]);
}
void solve(int u,int n) {
	dfs(u,0,n);
	int p=min_element(s.begin(),s.end())->se;
	work(p);
	s.clear();
	vis[p]=1;
	for(auto v:adj[p]) {
		if(!vis[v]) {
			solve(v,size[v]<size[p]?size[v]:n-size[p]);
		}
	}
}
int main() {
	scanf("%d%d",&n,&k);
	for(ri i=1;i<=n;i++)scanf("%d",&a[i]);
	for(ri i=1;i<n;i++) {
		int x,y;
		scanf("%d%d",&x,&y);
		adj[x].emplace_back(y);
		adj[y].emplace_back(x);
	}
	solve(1,n);
	for(ri i=1;i<=n;i++)printf("%d ",ans[i]);
	puts("");
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 6608kb

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: 0ms
memory: 10088kb

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: 3ms
memory: 6568kb

input:

1 1
20

output:

20 

result:

ok 1 number(s): "20"

Test #4:

score: -100
Wrong Answer
time: 2ms
memory: 10236kb

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:

119 180 125 94 180 137 47 76 147 180 

result:

wrong answer 1st numbers differ - expected: '159', found: '119'