Submission #60197

# Submission time Handle Problem Language Result Execution time Memory
60197 2018-07-23T20:31:04 Z thiago4532 Chase (CEOI17_chase) C++17
0 / 100
1670 ms 487132 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int maxn = 1e5 + 10, maxk = 1e2 + 10;
vector<int> grafo[maxn];
int n, k;
ll gain[maxn], x[maxn], dp[2][maxn][maxk], dp2[2][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);
	}
}

inline void calcDp(int u, int p=0){
	for(int l=1;l<=k;l++)
		for(int x=0;x<2;x++) dp[x][u][l] = dp2[x][u][l] = 0;
	
	for(auto v : grafo[u]){
		if(v == p) continue;

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

			if(dp[0][u][l] < y) dp2[0][u][l] = dp[0][u][l], dp[0][u][l] = y;
			else if(dp2[0][u][l] < y) dp2[0][u][l] = y;

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

	}
}

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

	calcDp(u, p);
}

void rotate(int u, int p=0){
	ans[u] = max(dp[0][u][k], dp[1][u][k]);
	// cout << "ans(" << u << "): " << ans[u] << " " << max(dp2[0][u][k], dp2[1][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], x6[k+1], x7[k+1], x8[k+1];

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

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

			if(dp[1][u][l] == a) dp[1][u][l] = dp2[1][u][l];
			if(dp[0][u][l] == b) dp[0][u][l] = dp2[0][u][l];

			if(dp[0][u][l] != 0) dp[0][u][l] -= x[v];
		}

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

		calcDp(v);
		rotate(v, u);

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



	}
}


int 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);

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

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

	// 	resp2 = max({resp2, dp[0][i][k], dp[1][i][k]});
	// 	cout << "ans2(" << i << "): " << max(dp[0][i][k], dp[1][i][k]) << " " << max(dp2[0][i][k], dp2[1][i][k]) << "\n";
	// }

	cout << resp << "\n";
	// cout << resp2 << "\n";
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2772 KB Output is correct
2 Correct 6 ms 2792 KB Output is correct
3 Incorrect 4 ms 2996 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2772 KB Output is correct
2 Correct 6 ms 2792 KB Output is correct
3 Incorrect 4 ms 2996 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1670 ms 487132 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2772 KB Output is correct
2 Correct 6 ms 2792 KB Output is correct
3 Incorrect 4 ms 2996 KB Output isn't correct
4 Halted 0 ms 0 KB -