Submission #68672

# Submission time Handle Problem Language Result Execution time Memory
68672 2018-08-17T22:29:52 Z IvanC Election Campaign (JOI15_election_campaign) C++17
100 / 100
483 ms 28536 KB
#include <bits/stdc++.h>
#define LSOne(S) (S & (-S))
using namespace std;

const int MAXN = 1e5 + 10;
const int MAXL = 17;

vector<int> grafo[MAXN],perguntas[MAXN];
int N,M,tab[MAXN][2],pai[MAXN],nivel[MAXN],ini[MAXN],fim[MAXN],dfsPtr,e1[MAXN],e2[MAXN],e3[MAXN];
int ancestral[MAXN][MAXL];
long long bit[MAXN];

void update_bit(int pos,int delta){
	while(pos <= N){
		bit[pos] += delta;
		pos += LSOne(pos);
	}
}

long long read(int pos){

	long long ans = 0;
	while(pos > 0){
		ans += bit[pos];
		pos -= LSOne(pos);
	}

	return ans;

}

void dfs_build(int v,int p){

	pai[v] = p;
	nivel[v] = nivel[p] + 1;
	ini[v] = ++dfsPtr;

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

	fim[v] = dfsPtr;

}

void build_lca(){
	for(int i = 1;i<=N;i++) ancestral[i][0] = pai[i];

	for(int j = 1;j<MAXL;j++){
		for(int i = 1;i<=N;i++){
			int z = ancestral[i][j-1];
			if(z == 0) ancestral[i][j] = 0;
			else ancestral[i][j] = ancestral[z][j-1];
		}
	}

}

int LCA(int u,int v){

	if(nivel[u] < nivel[v]) swap(u,v);

	for(int i = MAXL - 1;i>=0;i--){
		int z = ancestral[u][i];
		if(nivel[z] >= nivel[v]){
			u = z;
		}
	}

	if(u == v) return u;

	for(int i = MAXL - 1;i>=0;i--){
		if(ancestral[u][i] != ancestral[v][i] && ancestral[u][i] != -1){
			u = ancestral[u][i];
			v = ancestral[v][i];
		}
	}

	return pai[u];
}

void dfs_ans(int v,int p){

	int temp0 = 0, best_delta = 0;

	for(int u : grafo[v]){
		if(u == p) continue;
		dfs_ans(u,v);
		temp0 += tab[u][1];
	}

	for(int i : perguntas[v]){
		int u = e1[i],w = e2[i],x = e3[i];
		long long meu_delta = 0;
		if(u != v) meu_delta += read(ini[u]);
		if(w != v) meu_delta += read(ini[w]);
		best_delta = max(best_delta, int(meu_delta + x) );
	}

	tab[v][0] = temp0;
	tab[v][1] = temp0 + best_delta;

	update_bit(ini[v], -best_delta);
	update_bit(fim[v] + 1, best_delta);

}

int main(){

	scanf("%d",&N);
	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);
	}

	dfs_build(1,0);
	build_lca();

	scanf("%d",&M);
	for(int i = 1;i<=M;i++){
		scanf("%d %d %d",&e1[i],&e2[i],&e3[i]);
		int ancestral = LCA(e1[i],e2[i]);
		perguntas[ancestral].push_back(i);
	}

	dfs_ans(1,0);

	printf("%d\n",max(tab[1][1],tab[1][0]));

	return 0;
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:111:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&N);
  ~~~~~^~~~~~~~~
election_campaign.cpp:114:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&u,&v);
   ~~~~~^~~~~~~~~~~~~~~
election_campaign.cpp:122:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&M);
  ~~~~~^~~~~~~~~
election_campaign.cpp:124:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d",&e1[i],&e2[i],&e3[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5224 KB Output is correct
3 Correct 6 ms 5224 KB Output is correct
4 Correct 7 ms 5312 KB Output is correct
5 Correct 115 ms 18224 KB Output is correct
6 Correct 68 ms 25944 KB Output is correct
7 Correct 137 ms 25944 KB Output is correct
8 Correct 92 ms 25944 KB Output is correct
9 Correct 122 ms 25944 KB Output is correct
10 Correct 96 ms 25944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 25944 KB Output is correct
2 Correct 6 ms 25944 KB Output is correct
3 Correct 7 ms 25944 KB Output is correct
4 Correct 277 ms 28216 KB Output is correct
5 Correct 178 ms 28396 KB Output is correct
6 Correct 186 ms 28396 KB Output is correct
7 Correct 172 ms 28396 KB Output is correct
8 Correct 198 ms 28420 KB Output is correct
9 Correct 185 ms 28420 KB Output is correct
10 Correct 167 ms 28420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 25944 KB Output is correct
2 Correct 6 ms 25944 KB Output is correct
3 Correct 7 ms 25944 KB Output is correct
4 Correct 277 ms 28216 KB Output is correct
5 Correct 178 ms 28396 KB Output is correct
6 Correct 186 ms 28396 KB Output is correct
7 Correct 172 ms 28396 KB Output is correct
8 Correct 198 ms 28420 KB Output is correct
9 Correct 185 ms 28420 KB Output is correct
10 Correct 167 ms 28420 KB Output is correct
11 Correct 18 ms 28420 KB Output is correct
12 Correct 190 ms 28420 KB Output is correct
13 Correct 187 ms 28420 KB Output is correct
14 Correct 168 ms 28420 KB Output is correct
15 Correct 174 ms 28420 KB Output is correct
16 Correct 200 ms 28444 KB Output is correct
17 Correct 247 ms 28444 KB Output is correct
18 Correct 186 ms 28444 KB Output is correct
19 Correct 180 ms 28444 KB Output is correct
20 Correct 212 ms 28444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 242 ms 28444 KB Output is correct
2 Correct 234 ms 28444 KB Output is correct
3 Correct 483 ms 28444 KB Output is correct
4 Correct 243 ms 28444 KB Output is correct
5 Correct 336 ms 28444 KB Output is correct
6 Correct 220 ms 28444 KB Output is correct
7 Correct 473 ms 28444 KB Output is correct
8 Correct 301 ms 28444 KB Output is correct
9 Correct 204 ms 28444 KB Output is correct
10 Correct 411 ms 28444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5224 KB Output is correct
3 Correct 6 ms 5224 KB Output is correct
4 Correct 7 ms 5312 KB Output is correct
5 Correct 115 ms 18224 KB Output is correct
6 Correct 68 ms 25944 KB Output is correct
7 Correct 137 ms 25944 KB Output is correct
8 Correct 92 ms 25944 KB Output is correct
9 Correct 122 ms 25944 KB Output is correct
10 Correct 96 ms 25944 KB Output is correct
11 Correct 9 ms 28444 KB Output is correct
12 Correct 9 ms 28444 KB Output is correct
13 Correct 8 ms 28444 KB Output is correct
14 Correct 9 ms 28444 KB Output is correct
15 Correct 8 ms 28444 KB Output is correct
16 Correct 9 ms 28444 KB Output is correct
17 Correct 7 ms 28444 KB Output is correct
18 Correct 8 ms 28444 KB Output is correct
19 Correct 7 ms 28444 KB Output is correct
20 Correct 8 ms 28444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5224 KB Output is correct
3 Correct 6 ms 5224 KB Output is correct
4 Correct 7 ms 5312 KB Output is correct
5 Correct 115 ms 18224 KB Output is correct
6 Correct 68 ms 25944 KB Output is correct
7 Correct 137 ms 25944 KB Output is correct
8 Correct 92 ms 25944 KB Output is correct
9 Correct 122 ms 25944 KB Output is correct
10 Correct 96 ms 25944 KB Output is correct
11 Correct 8 ms 25944 KB Output is correct
12 Correct 6 ms 25944 KB Output is correct
13 Correct 7 ms 25944 KB Output is correct
14 Correct 277 ms 28216 KB Output is correct
15 Correct 178 ms 28396 KB Output is correct
16 Correct 186 ms 28396 KB Output is correct
17 Correct 172 ms 28396 KB Output is correct
18 Correct 198 ms 28420 KB Output is correct
19 Correct 185 ms 28420 KB Output is correct
20 Correct 167 ms 28420 KB Output is correct
21 Correct 18 ms 28420 KB Output is correct
22 Correct 190 ms 28420 KB Output is correct
23 Correct 187 ms 28420 KB Output is correct
24 Correct 168 ms 28420 KB Output is correct
25 Correct 174 ms 28420 KB Output is correct
26 Correct 200 ms 28444 KB Output is correct
27 Correct 247 ms 28444 KB Output is correct
28 Correct 186 ms 28444 KB Output is correct
29 Correct 180 ms 28444 KB Output is correct
30 Correct 212 ms 28444 KB Output is correct
31 Correct 242 ms 28444 KB Output is correct
32 Correct 234 ms 28444 KB Output is correct
33 Correct 483 ms 28444 KB Output is correct
34 Correct 243 ms 28444 KB Output is correct
35 Correct 336 ms 28444 KB Output is correct
36 Correct 220 ms 28444 KB Output is correct
37 Correct 473 ms 28444 KB Output is correct
38 Correct 301 ms 28444 KB Output is correct
39 Correct 204 ms 28444 KB Output is correct
40 Correct 411 ms 28444 KB Output is correct
41 Correct 9 ms 28444 KB Output is correct
42 Correct 9 ms 28444 KB Output is correct
43 Correct 8 ms 28444 KB Output is correct
44 Correct 9 ms 28444 KB Output is correct
45 Correct 8 ms 28444 KB Output is correct
46 Correct 9 ms 28444 KB Output is correct
47 Correct 7 ms 28444 KB Output is correct
48 Correct 8 ms 28444 KB Output is correct
49 Correct 7 ms 28444 KB Output is correct
50 Correct 8 ms 28444 KB Output is correct
51 Correct 262 ms 28444 KB Output is correct
52 Correct 212 ms 28444 KB Output is correct
53 Correct 428 ms 28444 KB Output is correct
54 Correct 185 ms 28444 KB Output is correct
55 Correct 237 ms 28444 KB Output is correct
56 Correct 224 ms 28444 KB Output is correct
57 Correct 336 ms 28444 KB Output is correct
58 Correct 202 ms 28444 KB Output is correct
59 Correct 269 ms 28444 KB Output is correct
60 Correct 211 ms 28444 KB Output is correct
61 Correct 290 ms 28444 KB Output is correct
62 Correct 180 ms 28444 KB Output is correct
63 Correct 271 ms 28444 KB Output is correct
64 Correct 186 ms 28444 KB Output is correct
65 Correct 331 ms 28444 KB Output is correct
66 Correct 171 ms 28444 KB Output is correct
67 Correct 210 ms 28444 KB Output is correct
68 Correct 218 ms 28536 KB Output is correct
69 Correct 306 ms 28536 KB Output is correct
70 Correct 213 ms 28536 KB Output is correct