Submission #70919

# Submission time Handle Problem Language Result Execution time Memory
70919 2018-08-23T16:57:33 Z zadrga Election Campaign (JOI15_election_campaign) C++14
100 / 100
458 ms 33484 KB
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF (1LL << 55)
#define MOD (1000 * 1000 * 1000 + 7)
#define maxn 100111
#define logn 20

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;

vector<int> adj[maxn];
vector<vector<int> > query[maxn];

int in[maxn], out[maxn], dep[maxn];
int par[maxn][logn];
ll dp[maxn];
int cnt;

struct BIT{
	ll bit[maxn];

	int query(int x){
		ll ret = 0;
		while(x){
			ret += bit[x];
			x -= (x & -x);
		}
		return ret;
	}

	void update(int x, ll val){
		while(x < maxn){
			bit[x] += val;
			x += (x & -x);
		}
	}
} bit;

void DFS(int x, int p){
	in[x] = ++cnt;
	par[x][0] = p;

	for(int v : adj[x]){
		if(v == p)
			continue;

		dep[v] = dep[x] + 1;
		DFS(v, x);
	}

	out[x] = cnt;
}

int LCA(int a, int b){
	if(dep[a] > dep[b])
		swap(a, b);

	int raz = dep[b] - dep[a];
	for(int i = 0; i < logn; i++){
		if((raz >> i) & 1)
			b = par[b][i]; 
	}

	if(a == b)
		return a;

	for(int i = logn - 1; i >= 0; i--){
		if(par[a][i] != par[b][i]){
			a = par[a][i];
			b = par[b][i];
		}
	}
	return par[a][0];
}

void solve(int x, int p){
	dp[x] = 0;
	ll sum = 0;
	for(int v : adj[x]){
		if(v == p)
			continue;

		solve(v, x);
		sum += dp[v];
	}

	dp[x] = sum;
	for(vector<int> a : query[x]){
		int u = a[0], v = a[1], cost = a[2];
		ll cur = 0;
	//	if(u != x)
			cur += bit.query(in[u]);
	//	if(v != x)
			cur += bit.query(in[v]);

		dp[x] = max(dp[x], sum + cur + cost);
	}

	ll cur = sum - dp[x];
	bit.update(in[x], cur);
	bit.update(out[x] + 1, -cur);
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n - 1; i++){
		int a, b;
		scanf("%d%d", &a, &b);
		adj[a].pb(b);
		adj[b].pb(a);
	}

	DFS(1, 0);

	for(int j = 1; j < logn; j++)
		for(int i = 1; i <= n; i++)
			par[i][j] = par[par[i][j - 1]][j - 1];

	int m;
	scanf("%d", &m);
	for(int i = 0; i < m; i++){
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		int lca = LCA(a, b);
		query[lca].pb({a, b, c});
	}

	solve(1, 0);

	printf("%lld\n", dp[1]);
	return 0;
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:113:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:116:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~
election_campaign.cpp:128:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &m);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:131:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &a, &b, &c);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 8 ms 5228 KB Output is correct
3 Correct 7 ms 5288 KB Output is correct
4 Correct 8 ms 5288 KB Output is correct
5 Correct 163 ms 19152 KB Output is correct
6 Correct 77 ms 26708 KB Output is correct
7 Correct 150 ms 26708 KB Output is correct
8 Correct 153 ms 26708 KB Output is correct
9 Correct 173 ms 26708 KB Output is correct
10 Correct 154 ms 26708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 26708 KB Output is correct
2 Correct 8 ms 26708 KB Output is correct
3 Correct 8 ms 26708 KB Output is correct
4 Correct 261 ms 33276 KB Output is correct
5 Correct 256 ms 33372 KB Output is correct
6 Correct 215 ms 33372 KB Output is correct
7 Correct 233 ms 33444 KB Output is correct
8 Correct 272 ms 33444 KB Output is correct
9 Correct 208 ms 33444 KB Output is correct
10 Correct 244 ms 33444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 26708 KB Output is correct
2 Correct 8 ms 26708 KB Output is correct
3 Correct 8 ms 26708 KB Output is correct
4 Correct 261 ms 33276 KB Output is correct
5 Correct 256 ms 33372 KB Output is correct
6 Correct 215 ms 33372 KB Output is correct
7 Correct 233 ms 33444 KB Output is correct
8 Correct 272 ms 33444 KB Output is correct
9 Correct 208 ms 33444 KB Output is correct
10 Correct 244 ms 33444 KB Output is correct
11 Correct 25 ms 33444 KB Output is correct
12 Correct 228 ms 33444 KB Output is correct
13 Correct 239 ms 33484 KB Output is correct
14 Correct 206 ms 33484 KB Output is correct
15 Correct 246 ms 33484 KB Output is correct
16 Correct 225 ms 33484 KB Output is correct
17 Correct 254 ms 33484 KB Output is correct
18 Correct 322 ms 33484 KB Output is correct
19 Correct 220 ms 33484 KB Output is correct
20 Correct 224 ms 33484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 337 ms 33484 KB Output is correct
2 Correct 236 ms 33484 KB Output is correct
3 Correct 343 ms 33484 KB Output is correct
4 Correct 206 ms 33484 KB Output is correct
5 Correct 386 ms 33484 KB Output is correct
6 Correct 227 ms 33484 KB Output is correct
7 Correct 457 ms 33484 KB Output is correct
8 Correct 386 ms 33484 KB Output is correct
9 Correct 277 ms 33484 KB Output is correct
10 Correct 458 ms 33484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 8 ms 5228 KB Output is correct
3 Correct 7 ms 5288 KB Output is correct
4 Correct 8 ms 5288 KB Output is correct
5 Correct 163 ms 19152 KB Output is correct
6 Correct 77 ms 26708 KB Output is correct
7 Correct 150 ms 26708 KB Output is correct
8 Correct 153 ms 26708 KB Output is correct
9 Correct 173 ms 26708 KB Output is correct
10 Correct 154 ms 26708 KB Output is correct
11 Correct 8 ms 33484 KB Output is correct
12 Correct 9 ms 33484 KB Output is correct
13 Correct 8 ms 33484 KB Output is correct
14 Correct 7 ms 33484 KB Output is correct
15 Correct 8 ms 33484 KB Output is correct
16 Correct 9 ms 33484 KB Output is correct
17 Correct 8 ms 33484 KB Output is correct
18 Correct 10 ms 33484 KB Output is correct
19 Correct 9 ms 33484 KB Output is correct
20 Correct 8 ms 33484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 8 ms 5228 KB Output is correct
3 Correct 7 ms 5288 KB Output is correct
4 Correct 8 ms 5288 KB Output is correct
5 Correct 163 ms 19152 KB Output is correct
6 Correct 77 ms 26708 KB Output is correct
7 Correct 150 ms 26708 KB Output is correct
8 Correct 153 ms 26708 KB Output is correct
9 Correct 173 ms 26708 KB Output is correct
10 Correct 154 ms 26708 KB Output is correct
11 Correct 7 ms 26708 KB Output is correct
12 Correct 8 ms 26708 KB Output is correct
13 Correct 8 ms 26708 KB Output is correct
14 Correct 261 ms 33276 KB Output is correct
15 Correct 256 ms 33372 KB Output is correct
16 Correct 215 ms 33372 KB Output is correct
17 Correct 233 ms 33444 KB Output is correct
18 Correct 272 ms 33444 KB Output is correct
19 Correct 208 ms 33444 KB Output is correct
20 Correct 244 ms 33444 KB Output is correct
21 Correct 25 ms 33444 KB Output is correct
22 Correct 228 ms 33444 KB Output is correct
23 Correct 239 ms 33484 KB Output is correct
24 Correct 206 ms 33484 KB Output is correct
25 Correct 246 ms 33484 KB Output is correct
26 Correct 225 ms 33484 KB Output is correct
27 Correct 254 ms 33484 KB Output is correct
28 Correct 322 ms 33484 KB Output is correct
29 Correct 220 ms 33484 KB Output is correct
30 Correct 224 ms 33484 KB Output is correct
31 Correct 337 ms 33484 KB Output is correct
32 Correct 236 ms 33484 KB Output is correct
33 Correct 343 ms 33484 KB Output is correct
34 Correct 206 ms 33484 KB Output is correct
35 Correct 386 ms 33484 KB Output is correct
36 Correct 227 ms 33484 KB Output is correct
37 Correct 457 ms 33484 KB Output is correct
38 Correct 386 ms 33484 KB Output is correct
39 Correct 277 ms 33484 KB Output is correct
40 Correct 458 ms 33484 KB Output is correct
41 Correct 8 ms 33484 KB Output is correct
42 Correct 9 ms 33484 KB Output is correct
43 Correct 8 ms 33484 KB Output is correct
44 Correct 7 ms 33484 KB Output is correct
45 Correct 8 ms 33484 KB Output is correct
46 Correct 9 ms 33484 KB Output is correct
47 Correct 8 ms 33484 KB Output is correct
48 Correct 10 ms 33484 KB Output is correct
49 Correct 9 ms 33484 KB Output is correct
50 Correct 8 ms 33484 KB Output is correct
51 Correct 372 ms 33484 KB Output is correct
52 Correct 312 ms 33484 KB Output is correct
53 Correct 440 ms 33484 KB Output is correct
54 Correct 225 ms 33484 KB Output is correct
55 Correct 309 ms 33484 KB Output is correct
56 Correct 248 ms 33484 KB Output is correct
57 Correct 321 ms 33484 KB Output is correct
58 Correct 243 ms 33484 KB Output is correct
59 Correct 315 ms 33484 KB Output is correct
60 Correct 246 ms 33484 KB Output is correct
61 Correct 363 ms 33484 KB Output is correct
62 Correct 213 ms 33484 KB Output is correct
63 Correct 278 ms 33484 KB Output is correct
64 Correct 247 ms 33484 KB Output is correct
65 Correct 442 ms 33484 KB Output is correct
66 Correct 243 ms 33484 KB Output is correct
67 Correct 238 ms 33484 KB Output is correct
68 Correct 245 ms 33484 KB Output is correct
69 Correct 327 ms 33484 KB Output is correct
70 Correct 285 ms 33484 KB Output is correct