Submission #64290

# Submission time Handle Problem Language Result Execution time Memory
64290 2018-08-04T00:00:03 Z RezwanArefin01 Election Campaign (JOI15_election_campaign) C++17
100 / 100
550 ms 153264 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii; 

const int N = 1e5 + 10; 

vector<int> adj[N], q[N];  
int a[N], b[N], c[N], n, m; 
int p[N][19], L[N], in[N], out[N], tym; 
ll bit[N + N], dp[N];

void dfs(int u, int par) {
	p[u][0] = par; 
	for(int i = 1; i <= 18; i++) 
		p[u][i] = p[p[u][i - 1]][i - 1]; 
	L[u] = L[par] + 1; 
	in[u] = ++tym; 
	for(int v : adj[u]) if(v - par) 
		dfs(v, u); 
	out[u] = ++tym; 
}

int lca(int u, int v) {
	if(L[u] < L[v]) swap(u, v); 
	for(int i = 18; i >= 0; i--) 
		if(L[p[u][i]] >= L[v]) 
			u = p[u][i];
	if(u == v) return u; 
	for(int i = 18; i >= 0; i--)
		if(p[u][i] - p[v][i]) 
			u = p[u][i], v = p[v][i];
	return p[u][0]; 
}

void update(int x, int v) {
	for(; x <= tym; x += x & -x) 
		bit[x] += v; 
}

ll read(int x) {
	ll ret = 0;
	for(; x > 0; x -= x & -x) 
		ret += bit[x];
	return ret;
}

void solve(int u, int par) {
	for(int v : adj[u]) if(v - par) {
		solve(v, u); 
		update(in[u], dp[v]);
		update(out[u], -dp[v]); 
	}
	ll x = read(in[u]), y = read(in[par]);
	for(int i : q[u]) {
		ll tot = read(in[a[i]]) + read(in[b[i]]) - x - y; 
		dp[u] = max(dp[u], tot + c[i]); 
	}
	dp[u] = max(dp[u], x - y);
	update(in[u], -dp[u]); 
	update(out[u], dp[u]); 
}
int main(int argc, char const *argv[]) {
	scanf("%d", &n);
	for(int i = 1; i < n; i++) {
		int u, v; 
		scanf("%d %d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	} dfs(1, 0); 

	scanf("%d", &m);
	for(int i = 1; i <= m; i++) {
		scanf("%d %d %d", &a[i], &b[i], &c[i]); 
		int l = lca(a[i], b[i]);
		q[l].push_back(i);  
	}
	solve(1, 0); 
	printf("%lld\n", dp[1]);
}

Compilation message

election_campaign.cpp: In function 'int main(int, const char**)':
election_campaign.cpp:65:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:68: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:73:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &m);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:75:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &a[i], &b[i], &c[i]); 
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 8 ms 5232 KB Output is correct
3 Correct 7 ms 5232 KB Output is correct
4 Correct 9 ms 5512 KB Output is correct
5 Correct 190 ms 20704 KB Output is correct
6 Correct 115 ms 29500 KB Output is correct
7 Correct 244 ms 29500 KB Output is correct
8 Correct 165 ms 29500 KB Output is correct
9 Correct 175 ms 29500 KB Output is correct
10 Correct 162 ms 29500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 29500 KB Output is correct
2 Correct 9 ms 29500 KB Output is correct
3 Correct 9 ms 29500 KB Output is correct
4 Correct 314 ms 39124 KB Output is correct
5 Correct 305 ms 41692 KB Output is correct
6 Correct 263 ms 44164 KB Output is correct
7 Correct 321 ms 46620 KB Output is correct
8 Correct 347 ms 49096 KB Output is correct
9 Correct 209 ms 51408 KB Output is correct
10 Correct 278 ms 53900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 29500 KB Output is correct
2 Correct 9 ms 29500 KB Output is correct
3 Correct 9 ms 29500 KB Output is correct
4 Correct 314 ms 39124 KB Output is correct
5 Correct 305 ms 41692 KB Output is correct
6 Correct 263 ms 44164 KB Output is correct
7 Correct 321 ms 46620 KB Output is correct
8 Correct 347 ms 49096 KB Output is correct
9 Correct 209 ms 51408 KB Output is correct
10 Correct 278 ms 53900 KB Output is correct
11 Correct 30 ms 53900 KB Output is correct
12 Correct 310 ms 57060 KB Output is correct
13 Correct 380 ms 59700 KB Output is correct
14 Correct 342 ms 62472 KB Output is correct
15 Correct 349 ms 65248 KB Output is correct
16 Correct 270 ms 67928 KB Output is correct
17 Correct 233 ms 70660 KB Output is correct
18 Correct 331 ms 73396 KB Output is correct
19 Correct 269 ms 76156 KB Output is correct
20 Correct 307 ms 78984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 361 ms 78984 KB Output is correct
2 Correct 313 ms 83868 KB Output is correct
3 Correct 502 ms 83868 KB Output is correct
4 Correct 240 ms 83868 KB Output is correct
5 Correct 389 ms 87784 KB Output is correct
6 Correct 267 ms 87784 KB Output is correct
7 Correct 394 ms 92268 KB Output is correct
8 Correct 370 ms 92268 KB Output is correct
9 Correct 207 ms 101092 KB Output is correct
10 Correct 502 ms 101092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 8 ms 5232 KB Output is correct
3 Correct 7 ms 5232 KB Output is correct
4 Correct 9 ms 5512 KB Output is correct
5 Correct 190 ms 20704 KB Output is correct
6 Correct 115 ms 29500 KB Output is correct
7 Correct 244 ms 29500 KB Output is correct
8 Correct 165 ms 29500 KB Output is correct
9 Correct 175 ms 29500 KB Output is correct
10 Correct 162 ms 29500 KB Output is correct
11 Correct 8 ms 101092 KB Output is correct
12 Correct 10 ms 101092 KB Output is correct
13 Correct 10 ms 101092 KB Output is correct
14 Correct 11 ms 101092 KB Output is correct
15 Correct 10 ms 101092 KB Output is correct
16 Correct 12 ms 101092 KB Output is correct
17 Correct 10 ms 101092 KB Output is correct
18 Correct 9 ms 101092 KB Output is correct
19 Correct 9 ms 101092 KB Output is correct
20 Correct 9 ms 101092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 8 ms 5232 KB Output is correct
3 Correct 7 ms 5232 KB Output is correct
4 Correct 9 ms 5512 KB Output is correct
5 Correct 190 ms 20704 KB Output is correct
6 Correct 115 ms 29500 KB Output is correct
7 Correct 244 ms 29500 KB Output is correct
8 Correct 165 ms 29500 KB Output is correct
9 Correct 175 ms 29500 KB Output is correct
10 Correct 162 ms 29500 KB Output is correct
11 Correct 10 ms 29500 KB Output is correct
12 Correct 9 ms 29500 KB Output is correct
13 Correct 9 ms 29500 KB Output is correct
14 Correct 314 ms 39124 KB Output is correct
15 Correct 305 ms 41692 KB Output is correct
16 Correct 263 ms 44164 KB Output is correct
17 Correct 321 ms 46620 KB Output is correct
18 Correct 347 ms 49096 KB Output is correct
19 Correct 209 ms 51408 KB Output is correct
20 Correct 278 ms 53900 KB Output is correct
21 Correct 30 ms 53900 KB Output is correct
22 Correct 310 ms 57060 KB Output is correct
23 Correct 380 ms 59700 KB Output is correct
24 Correct 342 ms 62472 KB Output is correct
25 Correct 349 ms 65248 KB Output is correct
26 Correct 270 ms 67928 KB Output is correct
27 Correct 233 ms 70660 KB Output is correct
28 Correct 331 ms 73396 KB Output is correct
29 Correct 269 ms 76156 KB Output is correct
30 Correct 307 ms 78984 KB Output is correct
31 Correct 361 ms 78984 KB Output is correct
32 Correct 313 ms 83868 KB Output is correct
33 Correct 502 ms 83868 KB Output is correct
34 Correct 240 ms 83868 KB Output is correct
35 Correct 389 ms 87784 KB Output is correct
36 Correct 267 ms 87784 KB Output is correct
37 Correct 394 ms 92268 KB Output is correct
38 Correct 370 ms 92268 KB Output is correct
39 Correct 207 ms 101092 KB Output is correct
40 Correct 502 ms 101092 KB Output is correct
41 Correct 8 ms 101092 KB Output is correct
42 Correct 10 ms 101092 KB Output is correct
43 Correct 10 ms 101092 KB Output is correct
44 Correct 11 ms 101092 KB Output is correct
45 Correct 10 ms 101092 KB Output is correct
46 Correct 12 ms 101092 KB Output is correct
47 Correct 10 ms 101092 KB Output is correct
48 Correct 9 ms 101092 KB Output is correct
49 Correct 9 ms 101092 KB Output is correct
50 Correct 9 ms 101092 KB Output is correct
51 Correct 429 ms 101092 KB Output is correct
52 Correct 328 ms 109312 KB Output is correct
53 Correct 550 ms 109312 KB Output is correct
54 Correct 277 ms 109312 KB Output is correct
55 Correct 357 ms 109420 KB Output is correct
56 Correct 296 ms 120212 KB Output is correct
57 Correct 489 ms 120212 KB Output is correct
58 Correct 291 ms 120212 KB Output is correct
59 Correct 382 ms 120712 KB Output is correct
60 Correct 316 ms 131420 KB Output is correct
61 Correct 474 ms 131420 KB Output is correct
62 Correct 263 ms 131420 KB Output is correct
63 Correct 366 ms 131504 KB Output is correct
64 Correct 332 ms 142420 KB Output is correct
65 Correct 405 ms 142420 KB Output is correct
66 Correct 279 ms 142420 KB Output is correct
67 Correct 358 ms 142420 KB Output is correct
68 Correct 267 ms 153264 KB Output is correct
69 Correct 440 ms 153264 KB Output is correct
70 Correct 326 ms 153264 KB Output is correct