QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#75543#4815. Flower's LandLG_MonkeyWA 2ms11716kbC++142.9kb2023-02-05 17:17:562023-02-05 17:17:59

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 17:17:59]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:11716kb
  • [2023-02-05 17:17:56]
  • 提交

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] = 1e12;
	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) {
//	cout << " dfs into " << u << "\n";
	nod = 0;
	getnod(u, 0);
	if (nod < k) return;
	mxd = 0;
	vis[u] = 1;
	FA[u] = 0;
	tim = 0; getdfn(u, 0);
	for (int i = 0; i <= mxd; i++) 
		for (int j = 0; j <= k + 1; j++) dp[i][j] = dp2[i][j] = -1e12;
	dp[1][1] = a[u];
	for (int i = 1; i <= mxd; i++)
		for (int j = 1; j <= k + 1; j++) {
			dp[i][j] = max(dp[dfn[FA[val[i]]]][j - 1] + a[val[i]], dp[i][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 + 1; j++) {
			dp2[i - 1][j + 1] = max(dp2[i - 1][j + 1], dp2[i][j] + a[val[i - 1]]);
//			cout << FA[val[i]] << " QAQ\n";
			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;
		res = max(res, dp[i][k]);
		res = max(res, dp2[i][k]);
		for (int l = 1; l <= k; l++) {
			int r = k + 1 - l;
			int L = l, R = 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;
} 

詳細信息

Test #1:

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

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: -100
Wrong Answer
time: 2ms
memory: 11716kb

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 37 21 39 31 

result:

wrong answer 4th numbers differ - expected: '31', found: '37'