답안 #35912

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
35912 2017-12-02T11:27:17 Z szawinis Chase (CEOI17_chase) C++14
0 / 100
0 ms 1840 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5+1, V = 102;
const ll INF = 1e17;

int n, lim, a[N];
ll ans, s[N], mx[N][V][2][2], dp[N][V][2][2]; // vertex, cnt, dir = down, up, last
vector<int> g[N];

void dfs(int u, int p) {
	ll mx[u][V][2][2];
	s[u] -= a[p];
	dp[u][0][0][0] = 0;
	dp[u][0][1][0] = 0;
	if(!p) dp[u][1][0][1] = mx[u][1][0][1] = s[u];
	dp[u][1][1][1] = mx[u][1][1][1] = s[u];
	ans = max(s[u] + a[p], ans);

	for(int v: g[u]) if(v != p) {
		dfs(v, u);
		for(int x = 0; x <= lim; x++) {
			ans = max(max(dp[v][x][0][0], dp[v][x][0][1]) + max(mx[u][lim-x][1][0], mx[u][lim-x][1][1] + a[v]), ans);
			ans = max(max(mx[u][lim-x][0][0], mx[u][lim-x][0][1]) + max(dp[v][x][1][0], dp[v][x][1][1] + a[u]), ans);
			// cout << u << ' ' << x << ' ' << v << ' ' << ans << endl;
			ll res[2][2] = {
				{
					max(dp[v][x][0][0], dp[v][x][0][1]),
					(x > 0 ? max(dp[v][x-1][0][0], dp[v][x-1][0][1]) + s[u]: -INF) // a[v] - a[v] cancel
				},
				{
					max(dp[v][x][1][0], dp[v][x][1][1] + a[u]),
					(x > 0 ? max(dp[v][x-1][1][0], dp[v][x-1][1][1] + a[u]) + s[u] - a[v] : -INF)
				}
			};
			for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) {
				dp[u][x][i][j] = max(res[i][j], dp[u][x][i][j]);
				if(dp[u][x][i][j] < 0) dp[u][x][i][j] = -INF;
				// cout << u << ' ' << x << ' ' << i << ' ' << j << ' ' << dp[u][x][i][j] << endl;
			}

			mx[u][x][0][0] = max(dp[u][x][0][0], mx[u][x][0][0]);
			mx[u][x][0][1] = max(dp[u][x][0][1] + a[p], mx[u][x][0][1]);
			mx[u][x][1][0] = max(dp[u][x][1][0], mx[u][x][1][0]);
			mx[u][x][1][1] = max(dp[u][x][1][1] + a[p], mx[u][x][1][1]);
		}
	}
}
// dp excludes parent, mx[u] doesn't

int main() {
	scanf("%d %d", &n, &lim);
	for(int i = 1; i <= n; i++) scanf("%d", a+i);
	for(int i = 1, u, v; i < n; i++) {
		scanf("%d %d", &u, &v);
		g[u].push_back(v);
		g[v].push_back(u);
		s[u] += a[v];
		s[v] += a[u];
	}
	for(int v = 0; v <= n; v++)
		for(int x = 0; x <= lim; x++)
			for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) dp[v][x][i][j] = mx[v][x][i][j] = -INF;
	dfs(1, 0);
	ans = 0;
	for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) ans = max(dp[1][lim][i][j], ans);
	cout << ans;
}

Compilation message

chase.cpp: In function 'int main()':
chase.cpp:52:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &lim);
                          ^
chase.cpp:53:46: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i = 1; i <= n; i++) scanf("%d", a+i);
                                              ^
chase.cpp:55:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &u, &v);
                         ^
# 결과 실행 시간 메모리 Grader output
1 Runtime error 0 ms 1840 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 0 ms 1840 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 0 ms 1840 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 0 ms 1840 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -