Submission #26470

# Submission time Handle Problem Language Result Execution time Memory
26470 2017-07-01T06:14:21 Z rubabredwan Election Campaign (JOI15_election_campaign) C++14
10 / 100
273 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] = max(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);
		assert(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]);
		assert(lc);
		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:105:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
                 ^
election_campaign.cpp:107: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:112:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &m);
                 ^
election_campaign.cpp:114: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 3 ms 32492 KB Output is correct
4 Incorrect 0 ms 32492 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 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 6 ms 32492 KB Output is correct
4 Correct 183 ms 45328 KB Output is correct
5 Correct 136 ms 45332 KB Output is correct
6 Correct 153 ms 45328 KB Output is correct
7 Correct 143 ms 45324 KB Output is correct
8 Correct 203 ms 45328 KB Output is correct
9 Correct 173 ms 45324 KB Output is correct
10 Correct 236 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 6 ms 32492 KB Output is correct
4 Correct 183 ms 45328 KB Output is correct
5 Correct 136 ms 45332 KB Output is correct
6 Correct 153 ms 45328 KB Output is correct
7 Correct 143 ms 45324 KB Output is correct
8 Correct 203 ms 45328 KB Output is correct
9 Correct 173 ms 45324 KB Output is correct
10 Correct 236 ms 45328 KB Output is correct
11 Correct 19 ms 33020 KB Output is correct
12 Correct 213 ms 45328 KB Output is correct
13 Correct 236 ms 45328 KB Output is correct
14 Correct 183 ms 45328 KB Output is correct
15 Correct 243 ms 45328 KB Output is correct
16 Correct 193 ms 45324 KB Output is correct
17 Correct 236 ms 45324 KB Output is correct
18 Correct 213 ms 45328 KB Output is correct
19 Correct 179 ms 45324 KB Output is correct
20 Correct 229 ms 45324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 273 ms 37532 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 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 3 ms 32492 KB Output is correct
4 Incorrect 0 ms 32492 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 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 3 ms 32492 KB Output is correct
4 Incorrect 0 ms 32492 KB Output isn't correct
5 Halted 0 ms 0 KB -