QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#408197#4815. Flower's Landchenxinyang2006WA 1ms7996kbC++202.6kb2024-05-09 20:11:412024-05-09 20:11:43

Judging History

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

  • [2024-05-09 20:11:43]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:7996kb
  • [2024-05-09 20:11:41]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define eb emplace_back
#define SZ(S) (int)S.size()
//#define mod 998244353
//#define mod 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3f
using namespace std;

template <class T>
void chkmax(T &x,T y){
	if(x < y) x = y;
}

template <class T>
void chkmin(T &x,T y){
	if(x > y) x = y;
}

inline int popcnt(int x){
	return __builtin_popcount(x);
}

inline int ctz(int x){
	return __builtin_ctz(x);
}


/*ll power(ll p,int k = mod - 2){
	ll ans = 1;
	while(k){
		if(k % 2 == 1) ans = ans * p % mod;
		p = p * p % mod;
		k /= 2;	
	}
	return ans;
}*/
int n,k;
int a[40005],answer[40005];

vector <int> G[40005];

vector <int> S;
int siz[40005],h[40005],vis[40005],dep[40005],ssum[40005];
void dfs1(int u,int f){
	S.eb(u);
	siz[u] = 1;h[u] = 0;
	for(int v:G[u]){
		if(v == f || vis[v]) continue;
		dfs1(v,u);
		siz[u] += siz[v];
		chkmax(h[u],siz[v]);
	} 
}

int dp[40005][3005],ndp[40005][3005];
void dfs2(int u,int f){
	dep[u] = dep[f] + 1;
	ssum[u] = ssum[f] + a[u];
	per(i,k,1) dp[u][i] = dp[u][i - 1] + a[u];
	dp[u][0] = -inf;

	for(int v:G[u]){
		if(v == f || vis[v]) continue;
		copy(dp[u],dp[u] + k + 1,dp[v]);
		dfs2(v,u);
		rep(i,1,k) chkmax(dp[u][i],dp[v][i]);
	}
}

void dfs3(int u,int f){
	per(i,k,1) ndp[u][i] = ndp[u][i - 1] + a[u];
	ndp[u][0] = -inf;
	rep(i,dep[u],k) chkmax(answer[u],dp[u][i] + ndp[u][k + dep[u] - i] - ssum[u]);

	reverse(G[u].begin(),G[u].end());
	for(int v:G[u]){
		if(v == f || vis[v]) continue;
		copy(ndp[u],ndp[u] + k + 1,ndp[v]);
		dfs3(v,u);
		rep(i,1,k) chkmax(ndp[u][i],ndp[v][i]);
	}
	reverse(G[u].begin(),G[u].end());
}

void solve(int rt){
	S.clear();
	dfs1(rt,0);
	int heavy = rt;
	for(int u:S) if(max(h[u],siz[rt] - siz[u]) <= max(h[heavy],siz[rt] - siz[heavy])) heavy = u;

	if(SZ(S) < k) return;
	fill(dp[heavy],dp[heavy] + k + 1,0);
	fill(ndp[heavy],ndp[heavy] + k + 1,0);
	dfs2(heavy,0);
	dfs3(heavy,0);

	vis[heavy] = 1;
	for(int u:G[rt]) if(!vis[u]) solve(u);
}

int main(){
//	freopen("test.in","r",stdin);
	scanf("%d%d",&n,&k);
	rep(u,1,n) scanf("%d",&a[u]);
	rep(i,1,n - 1){
		int u,v;
		scanf("%d%d",&u,&v);
		G[u].eb(v);G[v].eb(u);
	}
	solve(1);
	rep(u,1,n) printf("%d ",answer[u]);
	printf("\n");
	return 0;
}

详细

Test #1:

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

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

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

input:

1 1
20

output:

20 

result:

ok 1 number(s): "20"

Test #4:

score: -100
Wrong Answer
time: 1ms
memory: 7980kb

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 125 0 180 137 137 0 159 180 

result:

wrong answer 3rd numbers differ - expected: '169', found: '125'