Submission #61851

# Submission time Handle Problem Language Result Execution time Memory
61851 2018-07-26T21:47:42 Z IvanC Chase (CEOI17_chase) C++17
100 / 100
805 ms 358920 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MAXN = 1e5 + 10;
const int MAXC = 1e2 + 5;

vector<int> grafo[MAXN];
ll dp[MAXN][MAXC][2],P[MAXN],somatorio[MAXN],best;
ll copia[MAXN][MAXC],copia_sigma[MAXN],atual[MAXN][MAXC];
int N,C;

void adicona(int v,int u){
	for(int i = 1;i<=C;i++){
		if(atual[u][i] > dp[v][i][0]){
			dp[v][i][1] = dp[v][i][0];
			dp[v][i][0] = atual[u][i];
		}
		else if(atual[u][i] > dp[v][i][1]){
			dp[v][i][1] = atual[u][i];
		}
	}
	somatorio[v] += P[u];
}

void tira(int v,int u){
	for(int i = 1;i<=C;i++){
		if(atual[u][i] == dp[v][i][0]){
			dp[v][i][0] = dp[v][i][1];
		}
	}
	somatorio[v] -= P[u];
}

void faz_copia(int v){
	for(int i = 0;i<=C;i++){
		copia[v][i] = dp[v][i][0];
	}
	copia_sigma[v] = somatorio[v];
}

void restaura(int v){
	for(int i = 0;i<=C;i++){
		dp[v][i][0] = copia[v][i];
	}
	somatorio[v] = copia_sigma[v];
}

void calcula(int v){
	for(int i = 1;i<=C;i++){
		atual[v][i] = max(dp[v][i][0], dp[v][i-1][0] + somatorio[v] );
	}
}

void dfs1(int v,int p){
	
	for(int u : grafo[v]){
		if(u == p) continue;
		dfs1(u,v);
		adicona(v,u);
	}

	calcula(v);

}

void dfs2(int v,int p){

	calcula(v);
	faz_copia(v);
	for(int i = 0;i<=C;i++){
		best = max(best,atual[v][i]);
	}


	for(int u : grafo[v]){
		if(u == p) continue;
		tira(v,u);
		calcula(v);
		adicona(u,v);
		dfs2(u,v);
		restaura(v);
	}

}

int main(){

	scanf("%d %d",&N,&C);
	for(int i = 1;i<=N;i++){
		scanf("%lld",&P[i]);
	}
	for(int i = 1;i<N;i++){
		int u,v;
		scanf("%d %d",&u,&v);
		grafo[u].push_back(v);
		grafo[v].push_back(u);
	}

	dfs1(1,-1);
	dfs2(1,-1);

	printf("%lld\n",best);

	return 0;
}

Compilation message

chase.cpp: In function 'int main()':
chase.cpp:90:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&N,&C);
  ~~~~~^~~~~~~~~~~~~~~
chase.cpp:92:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld",&P[i]);
   ~~~~~^~~~~~~~~~~~~~
chase.cpp:96:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&u,&v);
   ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 2644 KB Output is correct
2 Correct 5 ms 2924 KB Output is correct
3 Correct 4 ms 2964 KB Output is correct
4 Correct 5 ms 2964 KB Output is correct
5 Correct 5 ms 2964 KB Output is correct
6 Correct 4 ms 2964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 2644 KB Output is correct
2 Correct 5 ms 2924 KB Output is correct
3 Correct 4 ms 2964 KB Output is correct
4 Correct 5 ms 2964 KB Output is correct
5 Correct 5 ms 2964 KB Output is correct
6 Correct 4 ms 2964 KB Output is correct
7 Correct 13 ms 6432 KB Output is correct
8 Correct 8 ms 6432 KB Output is correct
9 Correct 8 ms 6432 KB Output is correct
10 Correct 10 ms 6432 KB Output is correct
11 Correct 9 ms 6432 KB Output is correct
12 Correct 8 ms 6432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 784 ms 344184 KB Output is correct
2 Correct 787 ms 344268 KB Output is correct
3 Correct 553 ms 344268 KB Output is correct
4 Correct 538 ms 344268 KB Output is correct
5 Correct 732 ms 344268 KB Output is correct
6 Correct 720 ms 344268 KB Output is correct
7 Correct 758 ms 344268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 2644 KB Output is correct
2 Correct 5 ms 2924 KB Output is correct
3 Correct 4 ms 2964 KB Output is correct
4 Correct 5 ms 2964 KB Output is correct
5 Correct 5 ms 2964 KB Output is correct
6 Correct 4 ms 2964 KB Output is correct
7 Correct 13 ms 6432 KB Output is correct
8 Correct 8 ms 6432 KB Output is correct
9 Correct 8 ms 6432 KB Output is correct
10 Correct 10 ms 6432 KB Output is correct
11 Correct 9 ms 6432 KB Output is correct
12 Correct 8 ms 6432 KB Output is correct
13 Correct 784 ms 344184 KB Output is correct
14 Correct 787 ms 344268 KB Output is correct
15 Correct 553 ms 344268 KB Output is correct
16 Correct 538 ms 344268 KB Output is correct
17 Correct 732 ms 344268 KB Output is correct
18 Correct 720 ms 344268 KB Output is correct
19 Correct 758 ms 344268 KB Output is correct
20 Correct 752 ms 344268 KB Output is correct
21 Correct 344 ms 344268 KB Output is correct
22 Correct 805 ms 344268 KB Output is correct
23 Correct 515 ms 346084 KB Output is correct
24 Correct 716 ms 348132 KB Output is correct
25 Correct 520 ms 350268 KB Output is correct
26 Correct 679 ms 356668 KB Output is correct
27 Correct 690 ms 358920 KB Output is correct