Submission #60210

# Submission time Handle Problem Language Result Execution time Memory
60210 2018-07-23T20:45:05 Z thiago4532 Chase (CEOI17_chase) C++14
40 / 100
4000 ms 357128 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++){
			ll x = max(dp[0][v][l], dp[1][v][l]);
			ll 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(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;
		solve(v, u);
	
		for(int l=1;l<=k;l++){
			ll x = max(dp[0][v][l], dp[1][v][l]);
			ll 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 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";
	ll x3[maxk], x4[maxk], x5[maxk], x6[maxk], x7[maxk], x8[maxk];

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

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

		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++){
			ll a = max(dp[0][v][l], dp[1][v][l]);
			ll 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]);
	// }

	ll 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 4 ms 2680 KB Output is correct
2 Correct 4 ms 2788 KB Output is correct
3 Correct 6 ms 2864 KB Output is correct
4 Correct 7 ms 2940 KB Output is correct
5 Correct 6 ms 2940 KB Output is correct
6 Correct 6 ms 3056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2788 KB Output is correct
3 Correct 6 ms 2864 KB Output is correct
4 Correct 7 ms 2940 KB Output is correct
5 Correct 6 ms 2940 KB Output is correct
6 Correct 6 ms 3056 KB Output is correct
7 Correct 1154 ms 6588 KB Output is correct
8 Correct 132 ms 6672 KB Output is correct
9 Correct 96 ms 6672 KB Output is correct
10 Correct 2338 ms 6672 KB Output is correct
11 Correct 414 ms 6716 KB Output is correct
12 Correct 186 ms 6736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 4101 ms 357128 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2788 KB Output is correct
3 Correct 6 ms 2864 KB Output is correct
4 Correct 7 ms 2940 KB Output is correct
5 Correct 6 ms 2940 KB Output is correct
6 Correct 6 ms 3056 KB Output is correct
7 Correct 1154 ms 6588 KB Output is correct
8 Correct 132 ms 6672 KB Output is correct
9 Correct 96 ms 6672 KB Output is correct
10 Correct 2338 ms 6672 KB Output is correct
11 Correct 414 ms 6716 KB Output is correct
12 Correct 186 ms 6736 KB Output is correct
13 Execution timed out 4101 ms 357128 KB Time limit exceeded
14 Halted 0 ms 0 KB -