QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#72573#4815. Flower's LandFISHER_WA 3ms7832kbC++141.8kb2023-01-16 22:28:342023-01-16 22:28:35

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-16 22:28:35]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:7832kb
  • [2023-01-16 22:28:34]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int maxn = 30000, maxk = 3000;
int n, k;
int a[maxn + 5];
vector<int> g[maxn + 5];
bool vis[maxn + 5];
int siz[maxn + 5];
int ts, mn, rt;
void findRt(int u, int fa) {
	siz[u] = 1;
	int mx = 0;
	for (int v : g[u]) {
		if (v == fa || vis[v]) continue;
		findRt(v, u);
		siz[u] += siz[v], mx = max(mx, siz[v]);
	}
	mx = max(mx, ts - siz[u]);
	if (mx < mn) mn = mx, rt = u;
}
int rnk[maxn + 5], stamp;
void dfs(int u, int fa) {
	rnk[++stamp] = u, siz[u] = 1;
	for (int v : g[u]) {
		if (v == fa || vis[v]) continue;
		dfs(v, u);
		siz[u] += siz[v];
	}
}
int f[maxn + 5][maxk + 5], b[maxn + 5][maxk + 5];
int ans[maxn + 5];
void divide(int u, int tot) {
	if (tot < k) return;
	ts = tot, mn = INT_MAX, findRt(u, 0);
	vis[rt] = 1;
	stamp = 0, dfs(rt, 0);
	for (int i = 1; i <= tot + 1; i++) memset(f[i], 192, sizeof(f[i])), memset(b[i], 192, sizeof(b[i]));
	f[1][0] = 0;
	for (int i = 1; i <= tot; i++)
		for (int j = 0; j < k; j++) {
			f[i + 1][j + 1] = max(f[i + 1][j + 1], f[i][j] + a[rnk[i]]);
			f[i + siz[rnk[i]]][j] = max(f[i + siz[rnk[i]]][j], f[i][j]);
		}
	b[tot + 1][0] = 0;
	for (int i = tot; i > 1; i--)
		for (int j = 0; j < k; j++) {
			b[i][j] = max(b[i][j], b[i + 1][j - 1] + a[rnk[i]]);
			b[i][j] = max(b[i][j], b[i + siz[rnk[i]]][j]);
		}
	for (int i = 1; i <= tot; i++)
		for (int j = 0; j < k; j++) ans[rnk[i]] = max(ans[rnk[i]], f[i][j] + b[i + 1][k - 1 - j] + a[rnk[i]]);
	for (int v : g[u])
		if (!vis[v]) divide(v, siz[v]);
}
int main() {
	scanf("%d%d", &n, &k);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = 1; i < n; i++) {
		int u, v;
		scanf("%d%d", &u, &v);
		g[u].push_back(v), g[v].push_back(u);
	}
	divide(1, n);
	for (int 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: 3ms
memory: 6496kb

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: 7832kb

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

input:

1 1
20

output:

20 

result:

ok 1 number(s): "20"

Test #4:

score: -100
Wrong Answer
time: 3ms
memory: 6552kb

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 215 215 0 159 180 

result:

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