Submission #36012

# Submission time Handle Problem Language Result Execution time Memory
36012 2017-12-04T08:18:09 Z ngkan146 Election Campaign (JOI15_election_campaign) C++
100 / 100
646 ms 38688 KB
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct query{
	int a,b,val;
	query(int a=0,int b=0,int val=0): a(a), b(b), val(val) {}
};
int n,m,lv[100005], beg[100005], fin[100005], p[100005][20], ___time;
vector <int> G[100005];
vector <query> Q[100005];
void dfs(int u){
	for(int k=1;k<20;k++)
		p[u][k] = p[p[u][k-1]][k-1];
	beg[u] = ++___time;
	for(int i=0;i<(int)G[u].size();i++){
		int v = G[u][i];
		if (v == p[u][0]) continue;
		p[v][0] = u;
		lv[v] = lv[u]+1;
		dfs(v);
	}
	fin[u] = ___time;
}
int lca(int x,int y){
	for(int k=19;k>=0;k--)	if (lv[p[x][k]] >= lv[y]) x = p[x][k];
	for(int k=19;k>=0;k--)	if (lv[p[y][k]] >= lv[x]) y = p[y][k];
	if (x == y) return x;
	for(int k=19;k>=0;k--)	if (p[x][k] != p[y][k])	x = p[x][k], y = p[y][k];
	return p[x][0];
}
ll seg[400005], lazy[400005];
void _lazy(int id,int l,int r){
	seg[id] += lazy[id] * (r - l + 1);
	if (l != r){
		lazy[2*id] += lazy[id];
		lazy[2*id+1] += lazy[id];
	}
	lazy[id] = 0;
}
void seg_upd(int id,int l,int r,int u,int v,ll val){
	_lazy(id,l,r);
	if (r < u || v < l) return;
	if (u <= l && r <= v){
		lazy[id] += val;
		_lazy(id,l,r);
		return;
	}
	seg_upd(2*id, l, (l+r)/2, u, v, val);
	seg_upd(2*id+1, (l+r)/2+1, r, u, v, val);
	seg[id] = seg[2*id] + seg[2*id+1];
}
ll seg_get(int id,int l,int r,int pos){
	_lazy(id,l,r);
	if (r < pos || pos < l)		return 0;
	if (l == r)	return seg[id];
	return seg_get(2*id, l, (l+r)/2, pos) + seg_get(2*id+1, (l+r)/2+1, r, pos);
}
ll dp[100005];
ll get_decendant(int x,int y){
	int l = 0, r = G[x].size();
	while(l+1 < r){
		int mid = (l+r)/2;
		int z = G[x][mid];
		if (beg[y] < beg[z])	r = mid;
		else l = mid; 
	}
	return G[x][l];
}
void solve(int u){
	ll sum = 0;
	//cout << u << ' ' << p[u][0] << endl;
	for(int i=0;i<(int)G[u].size();i++){
		if (G[u][i] != p[u][0])	
			solve(G[u][i]), 
			sum += dp[G[u][i]];
	}
	
	dp[u] = sum;
	for(int id=0;id<(int)Q[u].size();id++){
		int a = Q[u][id].a;
		int b = Q[u][id].b;
		int val = Q[u][id].val;
		//cout << "Q" << u << ' ' << a << ' ' << b << endl;
		if (a == u)
			dp[u] = max(dp[u], seg_get(1,1,n,beg[b]) + sum - dp[get_decendant(u,b)] + val); 
		else if (b == u)
			dp[u] = max(dp[u], seg_get(1,1,n,beg[a]) + sum - dp[get_decendant(u,a)] + val);
		else
			dp[u] = max(dp[u], sum + seg_get(1,1,n,beg[a]) + seg_get(1,1,n,beg[b]) - dp[get_decendant(u,a)] - dp[get_decendant(u,b)] + val);
	}
	for(int i=0;i<(int)G[u].size();i++){
		int v = G[u][i];
		if (v == p[u][0]) continue;
		seg_upd(1,1,n,beg[v],fin[v], sum - dp[v]);
	}
	seg_upd(1,1,n,beg[u],beg[u], sum);
	//cout << "res " << u << ' ' << dp[u] << endl;
	
}
bool cmp(int x,int y){
	return beg[x] < beg[y];
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<n;i++){
		int x,y;
		scanf("%d %d",&x,&y);
		G[x].push_back(y);
		G[y].push_back(x);
	}
	lv[1] = 1;
	dfs(1);
	scanf("%d",&m);
	for(int i=1;i<=m;i++){
		int a,b,val;
		scanf("%d %d %d",&a,&b,&val);
		Q[lca(a,b)].push_back(query(a,b,val));
	}
	for(int i=1;i<=n;i++)	sort(G[i].begin(), G[i].end(), cmp);
	solve(1);
	cout << dp[1];
}
/*
7
3 4
6 5
2 7
1 5
7 5
4 5
5
4 3 10
5 6 5
2 6 9
7 2 2
1 3 8
*/

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:104:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
                ^
election_campaign.cpp:107:23: 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:113:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&m);
                ^
election_campaign.cpp:116:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d",&a,&b,&val);
                               ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 22724 KB Output is correct
2 Correct 0 ms 22724 KB Output is correct
3 Correct 0 ms 22724 KB Output is correct
4 Correct 0 ms 22724 KB Output is correct
5 Correct 266 ms 26024 KB Output is correct
6 Correct 129 ms 36704 KB Output is correct
7 Correct 293 ms 32928 KB Output is correct
8 Correct 236 ms 26544 KB Output is correct
9 Correct 286 ms 30760 KB Output is correct
10 Correct 229 ms 26484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 22724 KB Output is correct
2 Correct 0 ms 22724 KB Output is correct
3 Correct 3 ms 22724 KB Output is correct
4 Correct 359 ms 38684 KB Output is correct
5 Correct 343 ms 38684 KB Output is correct
6 Correct 343 ms 38688 KB Output is correct
7 Correct 363 ms 38684 KB Output is correct
8 Correct 349 ms 38688 KB Output is correct
9 Correct 329 ms 38688 KB Output is correct
10 Correct 356 ms 38684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 22724 KB Output is correct
2 Correct 0 ms 22724 KB Output is correct
3 Correct 3 ms 22724 KB Output is correct
4 Correct 359 ms 38684 KB Output is correct
5 Correct 343 ms 38684 KB Output is correct
6 Correct 343 ms 38688 KB Output is correct
7 Correct 363 ms 38684 KB Output is correct
8 Correct 349 ms 38688 KB Output is correct
9 Correct 329 ms 38688 KB Output is correct
10 Correct 356 ms 38684 KB Output is correct
11 Correct 36 ms 23252 KB Output is correct
12 Correct 319 ms 38684 KB Output is correct
13 Correct 309 ms 38688 KB Output is correct
14 Correct 329 ms 38684 KB Output is correct
15 Correct 326 ms 38688 KB Output is correct
16 Correct 339 ms 38684 KB Output is correct
17 Correct 319 ms 38684 KB Output is correct
18 Correct 339 ms 38684 KB Output is correct
19 Correct 306 ms 38688 KB Output is correct
20 Correct 353 ms 38684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 496 ms 27764 KB Output is correct
2 Correct 333 ms 38688 KB Output is correct
3 Correct 539 ms 34500 KB Output is correct
4 Correct 393 ms 28484 KB Output is correct
5 Correct 493 ms 33820 KB Output is correct
6 Correct 356 ms 28820 KB Output is correct
7 Correct 646 ms 33428 KB Output is correct
8 Correct 469 ms 27740 KB Output is correct
9 Correct 269 ms 38688 KB Output is correct
10 Correct 606 ms 32352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 22724 KB Output is correct
2 Correct 0 ms 22724 KB Output is correct
3 Correct 0 ms 22724 KB Output is correct
4 Correct 0 ms 22724 KB Output is correct
5 Correct 266 ms 26024 KB Output is correct
6 Correct 129 ms 36704 KB Output is correct
7 Correct 293 ms 32928 KB Output is correct
8 Correct 236 ms 26544 KB Output is correct
9 Correct 286 ms 30760 KB Output is correct
10 Correct 229 ms 26484 KB Output is correct
11 Correct 3 ms 22856 KB Output is correct
12 Correct 3 ms 22856 KB Output is correct
13 Correct 3 ms 22856 KB Output is correct
14 Correct 0 ms 22856 KB Output is correct
15 Correct 3 ms 22856 KB Output is correct
16 Correct 6 ms 22856 KB Output is correct
17 Correct 6 ms 22860 KB Output is correct
18 Correct 3 ms 22856 KB Output is correct
19 Correct 0 ms 22856 KB Output is correct
20 Correct 3 ms 22856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 22724 KB Output is correct
2 Correct 0 ms 22724 KB Output is correct
3 Correct 0 ms 22724 KB Output is correct
4 Correct 0 ms 22724 KB Output is correct
5 Correct 266 ms 26024 KB Output is correct
6 Correct 129 ms 36704 KB Output is correct
7 Correct 293 ms 32928 KB Output is correct
8 Correct 236 ms 26544 KB Output is correct
9 Correct 286 ms 30760 KB Output is correct
10 Correct 229 ms 26484 KB Output is correct
11 Correct 0 ms 22724 KB Output is correct
12 Correct 0 ms 22724 KB Output is correct
13 Correct 3 ms 22724 KB Output is correct
14 Correct 359 ms 38684 KB Output is correct
15 Correct 343 ms 38684 KB Output is correct
16 Correct 343 ms 38688 KB Output is correct
17 Correct 363 ms 38684 KB Output is correct
18 Correct 349 ms 38688 KB Output is correct
19 Correct 329 ms 38688 KB Output is correct
20 Correct 356 ms 38684 KB Output is correct
21 Correct 36 ms 23252 KB Output is correct
22 Correct 319 ms 38684 KB Output is correct
23 Correct 309 ms 38688 KB Output is correct
24 Correct 329 ms 38684 KB Output is correct
25 Correct 326 ms 38688 KB Output is correct
26 Correct 339 ms 38684 KB Output is correct
27 Correct 319 ms 38684 KB Output is correct
28 Correct 339 ms 38684 KB Output is correct
29 Correct 306 ms 38688 KB Output is correct
30 Correct 353 ms 38684 KB Output is correct
31 Correct 496 ms 27764 KB Output is correct
32 Correct 333 ms 38688 KB Output is correct
33 Correct 539 ms 34500 KB Output is correct
34 Correct 393 ms 28484 KB Output is correct
35 Correct 493 ms 33820 KB Output is correct
36 Correct 356 ms 28820 KB Output is correct
37 Correct 646 ms 33428 KB Output is correct
38 Correct 469 ms 27740 KB Output is correct
39 Correct 269 ms 38688 KB Output is correct
40 Correct 606 ms 32352 KB Output is correct
41 Correct 3 ms 22856 KB Output is correct
42 Correct 3 ms 22856 KB Output is correct
43 Correct 3 ms 22856 KB Output is correct
44 Correct 0 ms 22856 KB Output is correct
45 Correct 3 ms 22856 KB Output is correct
46 Correct 6 ms 22856 KB Output is correct
47 Correct 6 ms 22860 KB Output is correct
48 Correct 3 ms 22856 KB Output is correct
49 Correct 0 ms 22856 KB Output is correct
50 Correct 3 ms 22856 KB Output is correct
51 Correct 446 ms 27760 KB Output is correct
52 Correct 349 ms 38688 KB Output is correct
53 Correct 546 ms 32528 KB Output is correct
54 Correct 373 ms 28288 KB Output is correct
55 Correct 449 ms 27992 KB Output is correct
56 Correct 329 ms 38684 KB Output is correct
57 Correct 519 ms 33140 KB Output is correct
58 Correct 389 ms 28316 KB Output is correct
59 Correct 456 ms 27824 KB Output is correct
60 Correct 339 ms 38688 KB Output is correct
61 Correct 499 ms 33304 KB Output is correct
62 Correct 399 ms 28912 KB Output is correct
63 Correct 533 ms 28064 KB Output is correct
64 Correct 356 ms 38688 KB Output is correct
65 Correct 609 ms 33500 KB Output is correct
66 Correct 349 ms 28384 KB Output is correct
67 Correct 426 ms 28496 KB Output is correct
68 Correct 306 ms 38688 KB Output is correct
69 Correct 506 ms 31424 KB Output is correct
70 Correct 399 ms 28584 KB Output is correct