Submission #122640

# Submission time Handle Problem Language Result Execution time Memory
122640 2019-06-28T21:06:23 Z sofhiasouza Chase (CEOI17_chase) C++14
30 / 100
696 ms 228392 KB
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long int ll;

const int maxn = 1e5+10;

vector < int > grafo[maxn];

int n, v, vet[maxn];
ll soma[maxn], dp[maxn][110], dp2[maxn][110], resp;

void dfs(int u, int pai)
{
	for(int i = 0 ; i < grafo[u].size() ; i++)
	{
		int k = grafo[u][i];
		if(k == pai) continue;

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

			if(dp[u][j] < val) 
			{
				dp2[u][j] = dp[u][j];
				dp[u][j] = val;
			}
			else dp2[u][j] = max(dp2[u][j], val);
		}
	}
}

void rotate(int u, int pai)
{
	for(int i = 0 ; i <= v ; i++) resp = max({resp, dp[u][i-1] + soma[u], dp[u][i]});

	for(auto k : grafo[u])
	{
		ll d1[110];

		if(k == pai) continue;
		for(int i = 1 ; i <= v ; i++)
		{
			d1[i] = dp[u][i];
			
			if(dp[u][i] == dp[k][i] or dp[u][i] == dp[k][i-1] + soma[k] - vet[u]) dp[u][i] = dp2[u][i];
		}

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

			if(dp[k][i] < val) 
			{
				dp2[k][i] = dp[k][i];
				dp[k][i] = val;
			}
			else dp2[k][i] = max(dp2[k][i], val);
		}

		//dp[0][v] = max(dp[k][v-1] + soma[k], dp[k][v]);

		//resp = max(resp, dp[0][v]);
		//cout << u << ' ' << k << ' ' << dp[k][v] << endl;

		rotate(k, u);

		for(int i = 1 ; i <= v ; i++) dp[u][i] = d1[i];
	}
}

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

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

	for(int i = 0 ; i < n-1 ; i++)
	{
		int a, b;

		cin >> a >> b;

		grafo[a].pb(b);
		grafo[b].pb(a);
	}

	for(int i = 1 ; i <= n ; i++)
	{
		for(int j = 0 ; j < grafo[i].size() ; j++)
		{
			soma[i] += vet[grafo[i][j]];
		}
	}

	grafo[0].pb(1);

	dfs(0, 0);
	//cout << resp << endl;
	rotate(1, 0);

	cout << resp << endl;
}

Compilation message

chase.cpp: In function 'void dfs(int, int)':
chase.cpp:15:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0 ; i < grafo[u].size() ; i++)
                  ~~^~~~~~~~~~~~~~~~~
chase.cpp: In function 'int main()':
chase.cpp:92:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j = 0 ; j < grafo[i].size() ; j++)
                   ~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 4 ms 2680 KB Output is correct
5 Incorrect 4 ms 2680 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 4 ms 2680 KB Output is correct
5 Incorrect 4 ms 2680 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 596 ms 228392 KB Output is correct
2 Correct 588 ms 228088 KB Output is correct
3 Correct 414 ms 179696 KB Output is correct
4 Correct 414 ms 179376 KB Output is correct
5 Correct 696 ms 179448 KB Output is correct
6 Correct 690 ms 179448 KB Output is correct
7 Correct 693 ms 179448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 4 ms 2680 KB Output is correct
5 Incorrect 4 ms 2680 KB Output isn't correct
6 Halted 0 ms 0 KB -