QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#75533#4815. Flower's LandLG_MonkeyWA 4ms11548kbC++142.7kb2023-02-05 16:38:182023-02-05 16:38:20

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-05 16:38:20]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:11548kb
  • [2023-02-05 16:38:18]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define mp make_pair
#define fi first
#define pb push_back
#define se second
#define int long long
int n, k, a[40010], ans[40010];
vector<int> g[40010];
bool vis[40010];
int nod, siz[40010], mx[40010], hvy;
int dfn[40010], tim, cnt[40010], mxd, val[40010], dp[40010][3010], dp2[40010][310], FA[40010];
void getnod(int u, int fa) {
	nod++;
	for (int i = 0; i < g[u].size(); i++) {
		int v = g[u][i];
		if (v == fa || vis[v]) continue;
		getnod(v, u);
	}
}
void chkhvy(int u, int fa) {
	siz[u] = 1;
	mx[u] = -1;
	for (int i = 0; i < g[u].size(); i++) {
		int v = g[u][i];
		if (v == fa || vis[v]) continue;
		chkhvy(v, u);
		siz[u] += siz[v];
		mx[u] = max(mx[u], siz[v]);
	}
	mx[u] = max(mx[u], nod - siz[u]);
	if (mx[u] > mx[hvy]) hvy = u;
}
int gethvy(int u) {
	nod = 0;
	getnod(u, 0);
	hvy = 0;
	mx[hvy] = -1;
	chkhvy(u, 0); 
	return hvy;
}
void getdfn(int u, int fa) {
	cnt[u] = 1;
	dfn[u] = ++tim;
	val[dfn[u]] = u;
	mxd = max(mxd, dfn[u]);
	for (int i = 0; i < g[u].size(); i++) {
		int v = g[u][i];
		if (vis[v] || v == fa) continue;
		FA[v] = u;
		getdfn(v, u);
		cnt[u] += cnt[v];
	}
}
void dfs(int u) {
	mxd = 0;
	vis[u] = 1;
	FA[u] = 0;
	tim = 0; getdfn(u, 0);
	for (int i = 1; i <= mxd; i++) 
		for (int j = 0; j <= k; j++) dp[i][j] = dp2[i][j] = 0;
	dp[1][1] = a[u];
	for (int i = 1; i <= mxd; i++)
		for (int j = 1; j <= k; j++) {
			dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + a[val[i + 1]]);
			dp[i + cnt[val[i]]][j + 1] = max(dp[i + cnt[val[i]]][j + 1], dp[i][j] + a[val[i + cnt[val[i]]]]);
		}
	dp2[mxd][1] = a[val[mxd]];
	for (int i = mxd; i >= 1; i--) {
		dp2[i][1] = max(dp2[i][1], a[val[i]]);
		for (int j = 1; j <= k; j++) {
			dp2[i - 1][j + 1] = max(dp2[i - 1][j + 1], dp2[i][j] + a[val[i - 1]]);
			dp2[dfn[FA[val[i]]]][j + 1] = max(dp2[dfn[FA[val[i]]]][j + 1], dp2[i][j] + a[FA[val[i]]]);
//			dp2[i + cnt[val2[i]]][j + 1] = max(dp2[i + cnt[val2[i]]][j + 1], dp[i][j] + a[val2[i + cnt[val2[i]]]]);
		}
	}
	for (int i = 1; i <= mxd; i++) {
		int res = 0;
		for (int l = 0; l <= k; l++) {
			int r = k - l;
			int L = l, R = r;
			if (L < R) L++; else R++;
			res = max(res, dp[i][L] + dp2[i][R] - a[val[i]]);
		}
		ans[val[i]] = max(ans[val[i]], res);
	}
	for (int i = 0; i < g[u].size(); i++) {
		int v = g[u][i];
		if (vis[v]) continue;
		dfs(gethvy(v));
	} 
} 
signed main() {
	cin >> n >> k;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i < n; i++) {
		int u, v;
		cin >> u >> v;
		g[u].pb(v), g[v].pb(u);
	}
	dfs(gethvy(1));
	for (int i = 1; i <= n; i++) cout << ans[i] << " ";
	return 0;
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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: 4ms
memory: 11548kb

input:

1 1
20

output:

20 

result:

ok 1 number(s): "20"

Test #4:

score: 0
Accepted
time: 2ms
memory: 10688kb

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
Wrong Answer
time: 3ms
memory: 8336kb

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:

2508 2185 1790 1827 1870 1542 1960 1582 2373 2373 1854 1940 1793 1536 2508 1945 2508 1945 2039 1827 

result:

wrong answer 4th numbers differ - expected: '1945', found: '1827'