Submission #59212

# Submission time Handle Problem Language Result Execution time Memory
59212 2018-07-21T07:00:13 Z gusfring Election Campaign (JOI15_election_campaign) C++14
60 / 100
840 ms 70284 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];
	}
	return anc[u][0];
}
 
int dp[MAX], sum[MAX], c[MAX];
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;
	bool ok = true, ok2 = true;
	for(int i = 1, u, v; i < n; i++) {
		cin >> u >> v; --u; --v;
		if(u != v - 1) ok = false;
		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;
		if(w != 1) ok2 = false;
		paths[query_lca(u, v)].emplace_back(u, v, w);
	}
	if(n > 10000 && m > 10000 && (!ok && !ok2)) return 0;
	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 12 ms 12152 KB Output is correct
2 Correct 14 ms 12260 KB Output is correct
3 Correct 15 ms 12320 KB Output is correct
4 Correct 16 ms 12360 KB Output is correct
5 Correct 395 ms 49288 KB Output is correct
6 Correct 153 ms 49288 KB Output is correct
7 Correct 305 ms 49288 KB Output is correct
8 Correct 355 ms 49288 KB Output is correct
9 Correct 281 ms 49288 KB Output is correct
10 Correct 375 ms 49288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 49288 KB Output is correct
2 Correct 15 ms 49288 KB Output is correct
3 Correct 15 ms 49288 KB Output is correct
4 Correct 449 ms 49288 KB Output is correct
5 Correct 343 ms 49288 KB Output is correct
6 Correct 342 ms 49288 KB Output is correct
7 Correct 388 ms 49288 KB Output is correct
8 Correct 383 ms 49288 KB Output is correct
9 Correct 309 ms 49288 KB Output is correct
10 Correct 354 ms 49288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 49288 KB Output is correct
2 Correct 15 ms 49288 KB Output is correct
3 Correct 15 ms 49288 KB Output is correct
4 Correct 449 ms 49288 KB Output is correct
5 Correct 343 ms 49288 KB Output is correct
6 Correct 342 ms 49288 KB Output is correct
7 Correct 388 ms 49288 KB Output is correct
8 Correct 383 ms 49288 KB Output is correct
9 Correct 309 ms 49288 KB Output is correct
10 Correct 354 ms 49288 KB Output is correct
11 Correct 28 ms 49288 KB Output is correct
12 Correct 409 ms 49288 KB Output is correct
13 Correct 456 ms 49288 KB Output is correct
14 Correct 378 ms 49288 KB Output is correct
15 Correct 474 ms 49288 KB Output is correct
16 Correct 360 ms 49288 KB Output is correct
17 Correct 491 ms 49288 KB Output is correct
18 Correct 445 ms 49288 KB Output is correct
19 Correct 265 ms 49288 KB Output is correct
20 Correct 376 ms 49288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 698 ms 53544 KB Output is correct
2 Correct 332 ms 53544 KB Output is correct
3 Correct 628 ms 53544 KB Output is correct
4 Correct 484 ms 53544 KB Output is correct
5 Correct 462 ms 53544 KB Output is correct
6 Correct 420 ms 53544 KB Output is correct
7 Correct 784 ms 56424 KB Output is correct
8 Correct 607 ms 70284 KB Output is correct
9 Correct 353 ms 70284 KB Output is correct
10 Correct 840 ms 70284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12152 KB Output is correct
2 Correct 14 ms 12260 KB Output is correct
3 Correct 15 ms 12320 KB Output is correct
4 Correct 16 ms 12360 KB Output is correct
5 Correct 395 ms 49288 KB Output is correct
6 Correct 153 ms 49288 KB Output is correct
7 Correct 305 ms 49288 KB Output is correct
8 Correct 355 ms 49288 KB Output is correct
9 Correct 281 ms 49288 KB Output is correct
10 Correct 375 ms 49288 KB Output is correct
11 Correct 14 ms 70284 KB Output is correct
12 Correct 13 ms 70284 KB Output is correct
13 Correct 15 ms 70284 KB Output is correct
14 Correct 14 ms 70284 KB Output is correct
15 Correct 17 ms 70284 KB Output is correct
16 Correct 17 ms 70284 KB Output is correct
17 Correct 17 ms 70284 KB Output is correct
18 Correct 15 ms 70284 KB Output is correct
19 Correct 15 ms 70284 KB Output is correct
20 Correct 16 ms 70284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12152 KB Output is correct
2 Correct 14 ms 12260 KB Output is correct
3 Correct 15 ms 12320 KB Output is correct
4 Correct 16 ms 12360 KB Output is correct
5 Correct 395 ms 49288 KB Output is correct
6 Correct 153 ms 49288 KB Output is correct
7 Correct 305 ms 49288 KB Output is correct
8 Correct 355 ms 49288 KB Output is correct
9 Correct 281 ms 49288 KB Output is correct
10 Correct 375 ms 49288 KB Output is correct
11 Correct 13 ms 49288 KB Output is correct
12 Correct 15 ms 49288 KB Output is correct
13 Correct 15 ms 49288 KB Output is correct
14 Correct 449 ms 49288 KB Output is correct
15 Correct 343 ms 49288 KB Output is correct
16 Correct 342 ms 49288 KB Output is correct
17 Correct 388 ms 49288 KB Output is correct
18 Correct 383 ms 49288 KB Output is correct
19 Correct 309 ms 49288 KB Output is correct
20 Correct 354 ms 49288 KB Output is correct
21 Correct 28 ms 49288 KB Output is correct
22 Correct 409 ms 49288 KB Output is correct
23 Correct 456 ms 49288 KB Output is correct
24 Correct 378 ms 49288 KB Output is correct
25 Correct 474 ms 49288 KB Output is correct
26 Correct 360 ms 49288 KB Output is correct
27 Correct 491 ms 49288 KB Output is correct
28 Correct 445 ms 49288 KB Output is correct
29 Correct 265 ms 49288 KB Output is correct
30 Correct 376 ms 49288 KB Output is correct
31 Correct 698 ms 53544 KB Output is correct
32 Correct 332 ms 53544 KB Output is correct
33 Correct 628 ms 53544 KB Output is correct
34 Correct 484 ms 53544 KB Output is correct
35 Correct 462 ms 53544 KB Output is correct
36 Correct 420 ms 53544 KB Output is correct
37 Correct 784 ms 56424 KB Output is correct
38 Correct 607 ms 70284 KB Output is correct
39 Correct 353 ms 70284 KB Output is correct
40 Correct 840 ms 70284 KB Output is correct
41 Correct 14 ms 70284 KB Output is correct
42 Correct 13 ms 70284 KB Output is correct
43 Correct 15 ms 70284 KB Output is correct
44 Correct 14 ms 70284 KB Output is correct
45 Correct 17 ms 70284 KB Output is correct
46 Correct 17 ms 70284 KB Output is correct
47 Correct 17 ms 70284 KB Output is correct
48 Correct 15 ms 70284 KB Output is correct
49 Correct 15 ms 70284 KB Output is correct
50 Correct 16 ms 70284 KB Output is correct
51 Incorrect 303 ms 70284 KB Output isn't correct
52 Halted 0 ms 0 KB -