Submission #32084

# Submission time Handle Problem Language Result Execution time Memory
32084 2017-09-24T10:58:39 Z szawinis Election Campaign (JOI15_election_campaign) C++14
100 / 100
519 ms 53072 KB
#include <bits/stdc++.h>
using namespace std;
const int MAX = (1e5)+1;
 
int n, m;
vector<int> g[MAX];
vector<tuple<int,int,int> > paths[MAX];
 
int d[MAX];
vector<int> anc[MAX];
void init_lca(int u, int p) {
	for(int v: g[u]) if(v != p) {
		d[v] = d[u] + 1;
		anc[v].push_back(u);
		for(int j = 0; 1 << j+1 <= d[v]; j++)
			anc[v].push_back(anc[anc[v][j]][j]);
		init_lca(v, u);
	}
}
int query_lca(int u, int v) {
	if(d[u] > d[v]) {
		int diff = d[u] - d[v];
		for(int j = anc[u].size()-1; j >= 0; j--) if(diff >> j & 1) u = anc[u][j];
	} else if(d[u] < d[v]) {
		int diff = d[v] - d[u];
		for(int j = anc[v].size()-1; j >= 0; j--) if(diff >> j & 1) v = anc[v][j];
	}
	if(u == v) return u;
	for(int j = anc[u].size()-1; j >= 0; j--) if(j < anc[u].size() && anc[u][j] != anc[v][j]) {
		u = anc[u][j];
		v = anc[v][j];
	}
	assert(anc[u][0] == anc[v][0]);
	return anc[u][0];
}
 
int dp[MAX], sum[MAX], c[MAX];
unordered_map<int, int> mpf[MAX];
void dfs(int u, int p) {
	for(int v: g[u]) if(v != p) {
		dfs(v, u);
		sum[u] += dp[v];
		if(mpf[v].size() > mpf[u].size()) {
			mpf[u].swap(mpf[v]);
			swap(c[u], c[v]);
		}
		for(auto pp: mpf[v]) mpf[u][pp.first] = pp.second + c[v] - c[u];
	}
	dp[u] = sum[u];
	for(auto path: paths[u]) {
		int a, b, w;
		tie(a, b, w) = path;
		int down1 = (a != u ? mpf[u][a] + c[u] : 0);
		int down2 = (b != u ? mpf[u][b] + c[u] : 0);
		dp[u] = max(sum[u] + down1 + down2 + w, dp[u]);
	}
	mpf[u][u] = -c[u];
	c[u] += sum[u] - dp[u];
}
 
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for(int i = 1, u, v; i < n; i++) {
		cin >> u >> v; --u; --v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	init_lca(0, -1);
	cin >> m;
	for(int i = 0, u, v, w; i < m; i++) {
		cin >> u >> v >> w; --u; --v;
		paths[query_lca(u, v)].emplace_back(u, v, w);
	}
	dfs(0, -1);
	cout << dp[0];
}

Compilation message

election_campaign.cpp: In function 'void init_lca(int, int)':
election_campaign.cpp:15:24: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   for(int j = 0; 1 << j+1 <= d[v]; j++)
                        ^
election_campaign.cpp: In function 'int query_lca(int, int)':
election_campaign.cpp:29:49: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int j = anc[u].size()-1; j >= 0; j--) if(j < anc[u].size() && anc[u][j] != anc[v][j]) {
                                                 ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16244 KB Output is correct
2 Correct 3 ms 16244 KB Output is correct
3 Correct 6 ms 16244 KB Output is correct
4 Correct 3 ms 16376 KB Output is correct
5 Correct 353 ms 49868 KB Output is correct
6 Correct 113 ms 42652 KB Output is correct
7 Correct 216 ms 39236 KB Output is correct
8 Correct 263 ms 35132 KB Output is correct
9 Correct 249 ms 38744 KB Output is correct
10 Correct 243 ms 35648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 16244 KB Output is correct
2 Correct 6 ms 16244 KB Output is correct
3 Correct 0 ms 16376 KB Output is correct
4 Correct 233 ms 44528 KB Output is correct
5 Correct 246 ms 44532 KB Output is correct
6 Correct 209 ms 44532 KB Output is correct
7 Correct 269 ms 44528 KB Output is correct
8 Correct 289 ms 44528 KB Output is correct
9 Correct 256 ms 44536 KB Output is correct
10 Correct 263 ms 44528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 16244 KB Output is correct
2 Correct 6 ms 16244 KB Output is correct
3 Correct 0 ms 16376 KB Output is correct
4 Correct 233 ms 44528 KB Output is correct
5 Correct 246 ms 44532 KB Output is correct
6 Correct 209 ms 44532 KB Output is correct
7 Correct 269 ms 44528 KB Output is correct
8 Correct 289 ms 44528 KB Output is correct
9 Correct 256 ms 44536 KB Output is correct
10 Correct 263 ms 44528 KB Output is correct
11 Correct 19 ms 16772 KB Output is correct
12 Correct 293 ms 44524 KB Output is correct
13 Correct 313 ms 44532 KB Output is correct
14 Correct 233 ms 44544 KB Output is correct
15 Correct 263 ms 44536 KB Output is correct
16 Correct 229 ms 44540 KB Output is correct
17 Correct 276 ms 44528 KB Output is correct
18 Correct 269 ms 44528 KB Output is correct
19 Correct 206 ms 44540 KB Output is correct
20 Correct 239 ms 44532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 489 ms 53004 KB Output is correct
2 Correct 199 ms 44532 KB Output is correct
3 Correct 516 ms 40952 KB Output is correct
4 Correct 349 ms 37112 KB Output is correct
5 Correct 389 ms 40996 KB Output is correct
6 Correct 363 ms 37124 KB Output is correct
7 Correct 496 ms 40720 KB Output is correct
8 Correct 493 ms 52468 KB Output is correct
9 Correct 243 ms 44536 KB Output is correct
10 Correct 496 ms 40448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16244 KB Output is correct
2 Correct 3 ms 16244 KB Output is correct
3 Correct 6 ms 16244 KB Output is correct
4 Correct 3 ms 16376 KB Output is correct
5 Correct 353 ms 49868 KB Output is correct
6 Correct 113 ms 42652 KB Output is correct
7 Correct 216 ms 39236 KB Output is correct
8 Correct 263 ms 35132 KB Output is correct
9 Correct 249 ms 38744 KB Output is correct
10 Correct 243 ms 35648 KB Output is correct
11 Correct 3 ms 16508 KB Output is correct
12 Correct 9 ms 16376 KB Output is correct
13 Correct 3 ms 16376 KB Output is correct
14 Correct 3 ms 16376 KB Output is correct
15 Correct 9 ms 16508 KB Output is correct
16 Correct 6 ms 16376 KB Output is correct
17 Correct 0 ms 16376 KB Output is correct
18 Correct 6 ms 16376 KB Output is correct
19 Correct 3 ms 16376 KB Output is correct
20 Correct 3 ms 16376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16244 KB Output is correct
2 Correct 3 ms 16244 KB Output is correct
3 Correct 6 ms 16244 KB Output is correct
4 Correct 3 ms 16376 KB Output is correct
5 Correct 353 ms 49868 KB Output is correct
6 Correct 113 ms 42652 KB Output is correct
7 Correct 216 ms 39236 KB Output is correct
8 Correct 263 ms 35132 KB Output is correct
9 Correct 249 ms 38744 KB Output is correct
10 Correct 243 ms 35648 KB Output is correct
11 Correct 0 ms 16244 KB Output is correct
12 Correct 6 ms 16244 KB Output is correct
13 Correct 0 ms 16376 KB Output is correct
14 Correct 233 ms 44528 KB Output is correct
15 Correct 246 ms 44532 KB Output is correct
16 Correct 209 ms 44532 KB Output is correct
17 Correct 269 ms 44528 KB Output is correct
18 Correct 289 ms 44528 KB Output is correct
19 Correct 256 ms 44536 KB Output is correct
20 Correct 263 ms 44528 KB Output is correct
21 Correct 19 ms 16772 KB Output is correct
22 Correct 293 ms 44524 KB Output is correct
23 Correct 313 ms 44532 KB Output is correct
24 Correct 233 ms 44544 KB Output is correct
25 Correct 263 ms 44536 KB Output is correct
26 Correct 229 ms 44540 KB Output is correct
27 Correct 276 ms 44528 KB Output is correct
28 Correct 269 ms 44528 KB Output is correct
29 Correct 206 ms 44540 KB Output is correct
30 Correct 239 ms 44532 KB Output is correct
31 Correct 489 ms 53004 KB Output is correct
32 Correct 199 ms 44532 KB Output is correct
33 Correct 516 ms 40952 KB Output is correct
34 Correct 349 ms 37112 KB Output is correct
35 Correct 389 ms 40996 KB Output is correct
36 Correct 363 ms 37124 KB Output is correct
37 Correct 496 ms 40720 KB Output is correct
38 Correct 493 ms 52468 KB Output is correct
39 Correct 243 ms 44536 KB Output is correct
40 Correct 496 ms 40448 KB Output is correct
41 Correct 3 ms 16508 KB Output is correct
42 Correct 9 ms 16376 KB Output is correct
43 Correct 3 ms 16376 KB Output is correct
44 Correct 3 ms 16376 KB Output is correct
45 Correct 9 ms 16508 KB Output is correct
46 Correct 6 ms 16376 KB Output is correct
47 Correct 0 ms 16376 KB Output is correct
48 Correct 6 ms 16376 KB Output is correct
49 Correct 3 ms 16376 KB Output is correct
50 Correct 3 ms 16376 KB Output is correct
51 Correct 506 ms 52024 KB Output is correct
52 Correct 276 ms 44532 KB Output is correct
53 Correct 503 ms 40556 KB Output is correct
54 Correct 326 ms 37460 KB Output is correct
55 Correct 469 ms 52684 KB Output is correct
56 Correct 289 ms 44532 KB Output is correct
57 Correct 409 ms 40660 KB Output is correct
58 Correct 293 ms 37092 KB Output is correct
59 Correct 483 ms 52656 KB Output is correct
60 Correct 246 ms 44528 KB Output is correct
61 Correct 429 ms 40728 KB Output is correct
62 Correct 353 ms 37884 KB Output is correct
63 Correct 519 ms 52368 KB Output is correct
64 Correct 279 ms 44532 KB Output is correct
65 Correct 513 ms 40832 KB Output is correct
66 Correct 309 ms 36688 KB Output is correct
67 Correct 473 ms 53072 KB Output is correct
68 Correct 273 ms 44524 KB Output is correct
69 Correct 429 ms 40132 KB Output is correct
70 Correct 313 ms 35956 KB Output is correct