Submission #122220

# Submission time Handle Problem Language Result Execution time Memory
122220 2019-06-27T21:11:42 Z sams Chase (CEOI17_chase) C++14
100 / 100
763 ms 254952 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int maxn = 1e5+10, maxk = 110;

int n, k;
ll dp[maxn][maxk], dp2[maxn][maxk], ant[maxn][maxk], ans, vis[maxn], vet[maxn];

vector <int> graph[maxn];

ll solve(int u, int f = 0)
{
	//if(c == 0) return dp[u][c] = 0LL;
	//if(dp[u][c] != -1) return dp[u][c];

	for(int v: graph[u])
	{	
		if(v == f) continue;
		solve(v, u);

		for(int i = 1 ; i <= k ; ++i)
		{
			ll val = max(dp[v][i-1] + vis[v] - vet[u], dp[v][i]);
		
			if(val > dp[u][i]) dp2[u][i] = dp[u][i], dp[u][i] = val;
			else if(val > dp2[u][i]) dp2[u][i] = val;
		}
	}
	
	//return dp[u][c];
}

void rotate(int u, int f = 0)
{
	for(int i = 1 ; i <= k ; ++i)
	{
		ans = max({ans, dp[u][i], dp[u][i-1] + vis[u]});
	}

	for(int v: graph[u])
	{
		if(v == f) continue;

		for(int i = 1 ; i <= k ; ++i)
		{
			ant[u][i] = dp[u][i];
			
			ll val = max(dp[v][i], dp[v][i-1] + vis[v] - vet[u]);

			if(val == dp[u][i])
			{
				dp[u][i] = dp2[u][i];
			}
		}

		for(int i = 1 ; i <= k ; ++i)
		{
			ll val = max(dp[u][i], dp[u][i-1] + vis[u] - vet[v]);

			if(val > dp[v][i]) dp2[v][i] = dp[v][i], dp[v][i] = val;
			else if(val > dp2[v][i]) dp2[v][i] = val;
		}

		rotate(v, u);

		for(int i = 1 ; i <= k ; ++i) dp[u][i] = ant[u][i];
	}
}

int main()
{
	cin >> n >> k;

	for(int i = 1 ; i <= n ; ++i) cin >> vet[i];

	for(int i = 1 ; i < n ; ++i)
	{
		int u, v;
		cin >> u >> v;

		graph[u].push_back(v);
		graph[v].push_back(u);
	}

	for(int i = 1 ; i <= n ; ++i)
	{
		for(int v: graph[i])
		{
			vis[i] += vet[v];
		}
	}

	//memset(dp, -1, sizeof dp);

	solve(1);
	rotate(1);

	cout << ans << '\n';
}

Compilation message

chase.cpp: In function 'll solve(int, int)':
chase.cpp:33:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 3 ms 2688 KB Output is correct
4 Correct 4 ms 2688 KB Output is correct
5 Correct 4 ms 2688 KB Output is correct
6 Correct 4 ms 2688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 3 ms 2688 KB Output is correct
4 Correct 4 ms 2688 KB Output is correct
5 Correct 4 ms 2688 KB Output is correct
6 Correct 4 ms 2688 KB Output is correct
7 Correct 7 ms 5036 KB Output is correct
8 Correct 7 ms 4992 KB Output is correct
9 Correct 7 ms 4480 KB Output is correct
10 Correct 8 ms 5248 KB Output is correct
11 Correct 7 ms 5248 KB Output is correct
12 Correct 7 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 557 ms 228204 KB Output is correct
2 Correct 532 ms 230188 KB Output is correct
3 Correct 337 ms 182036 KB Output is correct
4 Correct 398 ms 252520 KB Output is correct
5 Correct 734 ms 254888 KB Output is correct
6 Correct 754 ms 254808 KB Output is correct
7 Correct 756 ms 254712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 3 ms 2688 KB Output is correct
4 Correct 4 ms 2688 KB Output is correct
5 Correct 4 ms 2688 KB Output is correct
6 Correct 4 ms 2688 KB Output is correct
7 Correct 7 ms 5036 KB Output is correct
8 Correct 7 ms 4992 KB Output is correct
9 Correct 7 ms 4480 KB Output is correct
10 Correct 8 ms 5248 KB Output is correct
11 Correct 7 ms 5248 KB Output is correct
12 Correct 7 ms 5248 KB Output is correct
13 Correct 557 ms 228204 KB Output is correct
14 Correct 532 ms 230188 KB Output is correct
15 Correct 337 ms 182036 KB Output is correct
16 Correct 398 ms 252520 KB Output is correct
17 Correct 734 ms 254888 KB Output is correct
18 Correct 754 ms 254808 KB Output is correct
19 Correct 756 ms 254712 KB Output is correct
20 Correct 763 ms 254704 KB Output is correct
21 Correct 138 ms 9592 KB Output is correct
22 Correct 747 ms 254836 KB Output is correct
23 Correct 394 ms 252512 KB Output is correct
24 Correct 742 ms 254952 KB Output is correct
25 Correct 317 ms 181744 KB Output is correct
26 Correct 546 ms 230392 KB Output is correct
27 Correct 541 ms 230428 KB Output is correct