Submission #26471

# Submission time Handle Problem Language Result Execution time Memory
26471 2017-07-01T06:42:47 Z rubabredwan Election Campaign (JOI15_election_campaign) C++14
100 / 100
336 ms 45332 KB
/*  Bismillahir Rahmanir Rahim  */

#include <bits/stdc++.h>

#define debug(x) cout << #x << ' ' << x << endl

using namespace std;

const int N = 200005;

struct data{
	int u, v, c;
	data () {}
	data (int _u, int _v, int _c){
		u = _u, v = _v, c = _c;
	}
};

struct BIT{
	int t[N], n;
	void init(int _n){
		n = _n;
		memset(t, 0, sizeof(t));
	}
	void update(int pos, int val){
		while(pos <= n){
			t[pos] += val;
			pos += pos & -pos;
		}
	}
	int get(int pos){
		int ret = 0;
		while(pos){
			ret += t[pos];
			pos -= pos & -pos;
		}
		return ret;
	}
	int get(int l, int r){
		return get(r) - get(l - 1);
	}
};

int n;
int m; 
int A[N], B[N], C[N];
int dp[N];
vector <data> cnd[N];
vector <int> g[N];
int dep[N];
int pa[N][18];
int in[N], out[N], t;
BIT t1, t2;

void dfs(int at, int par){
	in[at] = ++t;
	pa[at][0] = par;
	for(int i = 1; i <= 16; ++i) pa[at][i] = pa[ pa[at][i-1] ][ i-1 ];
	for(auto u : g[at]){
		if(u != par){
			dep[u] = dep[at] + 1;
			dfs(u, at);
		}
	}
	out[at] = ++t;
}

int lca(int a, int b){
	if(dep[a] < dep[b]) swap(a, b);
	for(int i = 16; i >= 0; --i) if(dep[a] - (1 << i) >= dep[b]) a = pa[a][i];
	if(a == b) return a;
	for(int i = 16; i >= 0; --i) if(pa[a][i] != pa[b][i]) a = pa[a][i], b = pa[b][i];
	return pa[a][0];
}

void solve(int at, int par){
	int child_sum = 0;
	for(auto u : g[at]){
		if(u == par) continue;
		solve(u, at);
		dp[at] += dp[u];
		child_sum += dp[u];
	}
	for(auto f : cnd[at]){
		int u = f.u, v = f.v, c = f.c;
		int sum = child_sum;
		if(u == at){
			sum += t2.get(in[v]) - t1.get(in[v]);
		}
		else{
			sum += t2.get(in[u]) - t1.get(in[u]);
			sum += t2.get(in[v]) - t1.get(in[v]);
		}
		dp[at] = max(dp[at], c + sum);
	}
	t1.update(in[at], +dp[at]);
	t1.update(out[at], -dp[at]);
	t2.update(in[at], +child_sum);
	t2.update(out[at], -child_sum);
}

int main(){
	int x, y;
	scanf("%d", &n);
	for(int i = 1; i < n; ++i){
		scanf("%d %d", &x, &y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	dfs(1, 0);
	scanf("%d", &m);
	for(int i = 1; i <= m; ++i){
		scanf("%d %d %d", &A[i], &B[i], &C[i]);
		int lc = lca(A[i], B[i]);
		if(B[i] == lc) swap(A[i], B[i]);
		cnd[lc].push_back(data(A[i], B[i], C[i]));
	}
	t1.init(2 * n);
	t2.init(2 * n);
	solve(1, 0);
	printf("%d\n", dp[1]);
	return 0;
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:104:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
                 ^
election_campaign.cpp:106:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &x, &y);
                         ^
election_campaign.cpp:111:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &m);
                 ^
election_campaign.cpp:113:41: 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 3 ms 32492 KB Output is correct
2 Correct 0 ms 32492 KB Output is correct
3 Correct 0 ms 32492 KB Output is correct
4 Correct 0 ms 32492 KB Output is correct
5 Correct 86 ms 35792 KB Output is correct
6 Correct 46 ms 43348 KB Output is correct
7 Correct 139 ms 40652 KB Output is correct
8 Correct 109 ms 36312 KB Output is correct
9 Correct 129 ms 39104 KB Output is correct
10 Correct 79 ms 36252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 32492 KB Output is correct
2 Correct 6 ms 32492 KB Output is correct
3 Correct 0 ms 32492 KB Output is correct
4 Correct 203 ms 45328 KB Output is correct
5 Correct 206 ms 45328 KB Output is correct
6 Correct 146 ms 45328 KB Output is correct
7 Correct 199 ms 45328 KB Output is correct
8 Correct 186 ms 45332 KB Output is correct
9 Correct 173 ms 45328 KB Output is correct
10 Correct 189 ms 45328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 32492 KB Output is correct
2 Correct 6 ms 32492 KB Output is correct
3 Correct 0 ms 32492 KB Output is correct
4 Correct 203 ms 45328 KB Output is correct
5 Correct 206 ms 45328 KB Output is correct
6 Correct 146 ms 45328 KB Output is correct
7 Correct 199 ms 45328 KB Output is correct
8 Correct 186 ms 45332 KB Output is correct
9 Correct 173 ms 45328 KB Output is correct
10 Correct 189 ms 45328 KB Output is correct
11 Correct 19 ms 33020 KB Output is correct
12 Correct 176 ms 45324 KB Output is correct
13 Correct 199 ms 45332 KB Output is correct
14 Correct 176 ms 45328 KB Output is correct
15 Correct 189 ms 45324 KB Output is correct
16 Correct 199 ms 45332 KB Output is correct
17 Correct 196 ms 45328 KB Output is correct
18 Correct 183 ms 45324 KB Output is correct
19 Correct 169 ms 45332 KB Output is correct
20 Correct 206 ms 45324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 243 ms 37532 KB Output is correct
2 Correct 179 ms 45328 KB Output is correct
3 Correct 333 ms 42320 KB Output is correct
4 Correct 176 ms 38252 KB Output is correct
5 Correct 273 ms 41864 KB Output is correct
6 Correct 213 ms 38588 KB Output is correct
7 Correct 336 ms 41504 KB Output is correct
8 Correct 276 ms 37508 KB Output is correct
9 Correct 176 ms 45328 KB Output is correct
10 Correct 329 ms 40776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 32492 KB Output is correct
2 Correct 0 ms 32492 KB Output is correct
3 Correct 0 ms 32492 KB Output is correct
4 Correct 0 ms 32492 KB Output is correct
5 Correct 86 ms 35792 KB Output is correct
6 Correct 46 ms 43348 KB Output is correct
7 Correct 139 ms 40652 KB Output is correct
8 Correct 109 ms 36312 KB Output is correct
9 Correct 129 ms 39104 KB Output is correct
10 Correct 79 ms 36252 KB Output is correct
11 Correct 3 ms 32624 KB Output is correct
12 Correct 6 ms 32624 KB Output is correct
13 Correct 3 ms 32624 KB Output is correct
14 Correct 6 ms 32624 KB Output is correct
15 Correct 6 ms 32624 KB Output is correct
16 Correct 0 ms 32624 KB Output is correct
17 Correct 0 ms 32628 KB Output is correct
18 Correct 3 ms 32624 KB Output is correct
19 Correct 0 ms 32624 KB Output is correct
20 Correct 3 ms 32624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 32492 KB Output is correct
2 Correct 0 ms 32492 KB Output is correct
3 Correct 0 ms 32492 KB Output is correct
4 Correct 0 ms 32492 KB Output is correct
5 Correct 86 ms 35792 KB Output is correct
6 Correct 46 ms 43348 KB Output is correct
7 Correct 139 ms 40652 KB Output is correct
8 Correct 109 ms 36312 KB Output is correct
9 Correct 129 ms 39104 KB Output is correct
10 Correct 79 ms 36252 KB Output is correct
11 Correct 3 ms 32492 KB Output is correct
12 Correct 6 ms 32492 KB Output is correct
13 Correct 0 ms 32492 KB Output is correct
14 Correct 203 ms 45328 KB Output is correct
15 Correct 206 ms 45328 KB Output is correct
16 Correct 146 ms 45328 KB Output is correct
17 Correct 199 ms 45328 KB Output is correct
18 Correct 186 ms 45332 KB Output is correct
19 Correct 173 ms 45328 KB Output is correct
20 Correct 189 ms 45328 KB Output is correct
21 Correct 19 ms 33020 KB Output is correct
22 Correct 176 ms 45324 KB Output is correct
23 Correct 199 ms 45332 KB Output is correct
24 Correct 176 ms 45328 KB Output is correct
25 Correct 189 ms 45324 KB Output is correct
26 Correct 199 ms 45332 KB Output is correct
27 Correct 196 ms 45328 KB Output is correct
28 Correct 183 ms 45324 KB Output is correct
29 Correct 169 ms 45332 KB Output is correct
30 Correct 206 ms 45324 KB Output is correct
31 Correct 243 ms 37532 KB Output is correct
32 Correct 179 ms 45328 KB Output is correct
33 Correct 333 ms 42320 KB Output is correct
34 Correct 176 ms 38252 KB Output is correct
35 Correct 273 ms 41864 KB Output is correct
36 Correct 213 ms 38588 KB Output is correct
37 Correct 336 ms 41504 KB Output is correct
38 Correct 276 ms 37508 KB Output is correct
39 Correct 176 ms 45328 KB Output is correct
40 Correct 329 ms 40776 KB Output is correct
41 Correct 3 ms 32624 KB Output is correct
42 Correct 6 ms 32624 KB Output is correct
43 Correct 3 ms 32624 KB Output is correct
44 Correct 6 ms 32624 KB Output is correct
45 Correct 6 ms 32624 KB Output is correct
46 Correct 0 ms 32624 KB Output is correct
47 Correct 0 ms 32628 KB Output is correct
48 Correct 3 ms 32624 KB Output is correct
49 Correct 0 ms 32624 KB Output is correct
50 Correct 3 ms 32624 KB Output is correct
51 Correct 253 ms 37528 KB Output is correct
52 Correct 196 ms 45328 KB Output is correct
53 Correct 333 ms 40904 KB Output is correct
54 Correct 166 ms 38056 KB Output is correct
55 Correct 176 ms 37760 KB Output is correct
56 Correct 166 ms 45324 KB Output is correct
57 Correct 286 ms 41360 KB Output is correct
58 Correct 196 ms 38084 KB Output is correct
59 Correct 266 ms 37592 KB Output is correct
60 Correct 209 ms 45328 KB Output is correct
61 Correct 293 ms 41484 KB Output is correct
62 Correct 193 ms 38680 KB Output is correct
63 Correct 246 ms 37832 KB Output is correct
64 Correct 193 ms 45324 KB Output is correct
65 Correct 316 ms 41656 KB Output is correct
66 Correct 186 ms 38152 KB Output is correct
67 Correct 233 ms 38264 KB Output is correct
68 Correct 259 ms 45328 KB Output is correct
69 Correct 263 ms 40088 KB Output is correct
70 Correct 176 ms 38352 KB Output is correct