QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#72997#4815. Flower's LandFISHER_WA 2ms6580kbC++141.8kb2023-01-21 16:02:432023-01-21 16:02: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-21 16:02:43]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:6580kb
  • [2023-01-21 16:02:43]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int maxn = 40000, 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[u = rt] = 1;
	stamp = 0, dfs(u, 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][k - j]);
	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]);
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 6580kb

input:

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

output:

20 0 17 20 20 

result:

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