답안 #59715

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
59715 2018-07-23T00:26:54 Z thiago4532 Chase (CEOI17_chase) C++17
0 / 100
1080 ms 310664 KB
#include <bits/stdc++.h>
#define int long long

using namespace std;
const int maxn = 1e5 + 10, maxk = 1e2 + 10;
vector<int> grafo[maxn];
int n, k;
int gain[maxn], x[maxn], dp[maxn][maxk], dp2[maxn][maxk], ans[maxn];

void dfs(int u, int p=0){
	gain[u] = 0;
	for(auto v : grafo[u]){
		if(v == p) continue;
		gain[u] += x[v];

		dfs(v, u);
	}
}

void solve(int u, int p=0){
	for(int l=0;l<=k;l++)
		dp[u][l] = dp2[u][l] = 0;

	for(auto v : grafo[u]){
		if(v == p) continue;
		solve(v, u);

		for(int l=1;l<=k;l++){
			int x = max(dp[v][l], dp[v][l-1] + gain[u]);

			if(dp[u][l] < x) dp2[u][l] = dp[u][l], dp[u][l] = x;
			else if(dp2[u][l] < x) dp2[u][l] = x;
		}
	}
}


void rotate(int u, int p=0){
	ans[u] = dp[u][k];

	// cout << "Raiz em " << u << " com valor de dp " << dp[u][k] << " - " << dp2[u][k] << "\n";

	for(auto v : grafo[u]){
		if(v == p) continue;

		int x1 = gain[u], x2 = gain[v];

		int x3[k+1], x4[k+1], x5[k+1];
		for(int l=1;l<=k;l++) x3[l] = dp[u][l], x4[l] = dp[v][l], x5[l] = dp2[v][l];

		for(int l=1;l<=k;l++){
			if(dp[u][l] == dp[v][l] || dp[u][l] == dp[v][l-1] + gain[u]){
				dp[u][l] = dp2[u][l];
				if(dp[u][l] == x3[l]) dp[u][l] -= x[v];
			}
		}

		gain[u] -= x[v];
		gain[v] += x[u];

		for(int l=0;l<=k;l++)
			dp[v][l] = dp2[v][l] = 0;

		for(auto b : grafo[v]){
			for(int l=1;l<=k;l++){
				if(dp[v][l] < dp[b][l]) dp2[v][l] = dp[v][l], dp[v][l] = dp[b][l];
				else if(dp2[v][l] < dp[b][l]) dp2[v][l] = dp[b][l];

				if(dp[v][l] < dp[b][l-1] + gain[v]) dp2[v][l] = dp[v][l], dp[v][l] = dp[b][l-1] + gain[v];
				else if(dp2[v][l] < dp[b][l-1] + gain[v]) dp2[v][l] = dp[b][l-1] + gain[v];
			}
		}

		rotate(v, u);
		gain[u] = x1, gain[v] = x2;
		for(int l=1;l<=k;l++) dp[u][l] = x3[l], dp[v][l] = x4[l], dp2[v][l] = x5[l];

	}
}

int32_t main(){
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n >> k;
	for(int i=1;i<=n;i++)
		cin >> x[i];

	for(int i=1;i<n;i++){
		int a, b;
		cin >> a >> b;
		grafo[a].push_back(b);
		grafo[b].push_back(a);
	}

	dfs(1);
	solve(1);

	rotate(1);

	int resp=0;
	for(int i=1;i<=n;i++){
		// cout << "gain: " << gain[i] << "\n";
		// cout << "dp: " << dp[i][k] << "\n";
		 // cout << "ans(" << i << "): " << ans[i] << "\n";
		resp = max(resp, ans[i]);
	}

	// for(int i=1;i<=n;i++){
	// 	dfs(i);
	// 	solve(i);

	// 	 // cout << "ans2(" << i << "): " << dp[i][k] << " " << dp2[i][k] << "\n";
	// }

	cout << resp << "\n";
	
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2732 KB Output is correct
2 Incorrect 6 ms 2924 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2732 KB Output is correct
2 Incorrect 6 ms 2924 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1080 ms 310664 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2732 KB Output is correct
2 Incorrect 6 ms 2924 KB Output isn't correct
3 Halted 0 ms 0 KB -