Submission #122636

# Submission time Handle Problem Language Result Execution time Memory
122636 2019-06-28T20:55:19 Z sofhiasouza Chase (CEOI17_chase) C++14
30 / 100
724 ms 228256 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(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);

	resp = dp[0][v];
	//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:90: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 561 ms 228256 KB Output is correct
2 Correct 564 ms 227888 KB Output is correct
3 Correct 406 ms 179604 KB Output is correct
4 Correct 466 ms 179268 KB Output is correct
5 Correct 724 ms 179492 KB Output is correct
6 Correct 712 ms 179420 KB Output is correct
7 Correct 669 ms 179420 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 -