Submission #151969

# Submission time Handle Problem Language Result Execution time Memory
151969 2019-09-05T17:45:12 Z TadijaSebez Election Campaign (JOI15_election_campaign) C++11
100 / 100
401 ms 22984 KB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=100050;
vector<int> E[N];
int go[N][2],fa[N],sum[N],dp[N],sub[N];
void pull(int x){ sum[x]=sub[x]-dp[x]+sum[go[x][0]]+sum[go[x][1]];}
int pd(int x){ return go[fa[x]][0]==x?0:go[fa[x]][1]==x?1:-1;}
void rot(int x)
{
	assert(pd(x)!=-1);
	int y=fa[x],z=fa[y],p=pd(x),q=pd(y),w=go[x][p^1];
	if(~q) go[z][q]=x;go[x][p^1]=y;go[y][p]=w;
	if(w) fa[w]=y;fa[x]=z;fa[y]=x;
	pull(y);pull(x);
}
void splay(int x){ while(pd(x)!=-1){ if(pd(fa[x])!=-1) rot(pd(fa[x])==pd(x)?fa[x]:x);rot(x);}}
void access(int x){ for(splay(x),go[x][1]=0,pull(x);fa[x];rot(x)) splay(fa[x]),go[fa[x]][1]=x,pull(fa[x]);}
int get(int x, int y){ access(x);access(y);splay(x);return sum[x];}
int lca(int x, int y){ access(x);access(y);splay(x);return fa[x]?fa[x]:go[x][0]==y?y:x;}
int a[N],b[N],c[N];
vector<int> plan[N];
void DFS(int u, int p){ if(p) fa[u]=p;for(int v:E[u]) if(v!=p) DFS(v,u);}
void Solve(int u, int p)
{
	for(int v:E[u]) if(v!=p)
	{
		Solve(v,u);
		sub[u]+=dp[v];
	}
	dp[u]=sub[u];
	for(int i:plan[u])
	{
		int g=0;
		if(a[i]!=u) g+=get(a[i],u);
		if(b[i]!=u) g+=get(b[i],u);
		dp[u]=max(dp[u],sub[u]+g+c[i]);
	}
	pull(u);
}
int main()
{
	int n,m;
	scanf("%i",&n);
	for(int i=1,u,v;i<n;i++) scanf("%i %i",&u,&v),E[u].pb(v),E[v].pb(u);
	DFS(1,0);
	scanf("%i",&m);
	for(int i=1;i<=m;i++) scanf("%i %i %i",&a[i],&b[i],&c[i]),plan[lca(a[i],b[i])].pb(i);
	Solve(1,0);
	printf("%i\n",dp[1]);
	return 0;
}

Compilation message

election_campaign.cpp: In function 'void rot(int)':
election_campaign.cpp:13:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
  if(~q) go[z][q]=x;go[x][p^1]=y;go[y][p]=w;
  ^~
election_campaign.cpp:13:20: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
  if(~q) go[z][q]=x;go[x][p^1]=y;go[y][p]=w;
                    ^~
election_campaign.cpp:14:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
  if(w) fa[w]=y;fa[x]=z;fa[y]=x;
  ^~
election_campaign.cpp:14:16: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
  if(w) fa[w]=y;fa[x]=z;fa[y]=x;
                ^~
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:44:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i",&n);
  ~~~~~^~~~~~~~~
election_campaign.cpp:45:58: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1,u,v;i<n;i++) scanf("%i %i",&u,&v),E[u].pb(v),E[v].pb(u);
                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
election_campaign.cpp:47:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i",&m);
  ~~~~~^~~~~~~~~
election_campaign.cpp:48:59: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1;i<=m;i++) scanf("%i %i %i",&a[i],&b[i],&c[i]),plan[lca(a[i],b[i])].pb(i);
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 4984 KB Output is correct
4 Correct 6 ms 5112 KB Output is correct
5 Correct 85 ms 11548 KB Output is correct
6 Correct 56 ms 18936 KB Output is correct
7 Correct 84 ms 16872 KB Output is correct
8 Correct 57 ms 11128 KB Output is correct
9 Correct 81 ms 15276 KB Output is correct
10 Correct 57 ms 11080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5240 KB Output is correct
2 Correct 7 ms 5112 KB Output is correct
3 Correct 7 ms 5240 KB Output is correct
4 Correct 269 ms 22648 KB Output is correct
5 Correct 266 ms 22520 KB Output is correct
6 Correct 226 ms 22612 KB Output is correct
7 Correct 280 ms 22516 KB Output is correct
8 Correct 264 ms 22520 KB Output is correct
9 Correct 224 ms 22592 KB Output is correct
10 Correct 266 ms 22520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5240 KB Output is correct
2 Correct 7 ms 5112 KB Output is correct
3 Correct 7 ms 5240 KB Output is correct
4 Correct 269 ms 22648 KB Output is correct
5 Correct 266 ms 22520 KB Output is correct
6 Correct 226 ms 22612 KB Output is correct
7 Correct 280 ms 22516 KB Output is correct
8 Correct 264 ms 22520 KB Output is correct
9 Correct 224 ms 22592 KB Output is correct
10 Correct 266 ms 22520 KB Output is correct
11 Correct 34 ms 6008 KB Output is correct
12 Correct 273 ms 22984 KB Output is correct
13 Correct 289 ms 22812 KB Output is correct
14 Correct 228 ms 22780 KB Output is correct
15 Correct 268 ms 22904 KB Output is correct
16 Correct 229 ms 22876 KB Output is correct
17 Correct 269 ms 22732 KB Output is correct
18 Correct 272 ms 22788 KB Output is correct
19 Correct 228 ms 22776 KB Output is correct
20 Correct 270 ms 22848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 320 ms 14760 KB Output is correct
2 Correct 223 ms 22588 KB Output is correct
3 Correct 379 ms 20116 KB Output is correct
4 Correct 145 ms 14292 KB Output is correct
5 Correct 324 ms 19752 KB Output is correct
6 Correct 147 ms 14324 KB Output is correct
7 Correct 400 ms 19320 KB Output is correct
8 Correct 269 ms 15352 KB Output is correct
9 Correct 221 ms 22676 KB Output is correct
10 Correct 380 ms 18384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 4984 KB Output is correct
4 Correct 6 ms 5112 KB Output is correct
5 Correct 85 ms 11548 KB Output is correct
6 Correct 56 ms 18936 KB Output is correct
7 Correct 84 ms 16872 KB Output is correct
8 Correct 57 ms 11128 KB Output is correct
9 Correct 81 ms 15276 KB Output is correct
10 Correct 57 ms 11080 KB Output is correct
11 Correct 8 ms 5112 KB Output is correct
12 Correct 8 ms 5240 KB Output is correct
13 Correct 8 ms 5240 KB Output is correct
14 Correct 7 ms 5240 KB Output is correct
15 Correct 8 ms 5240 KB Output is correct
16 Correct 8 ms 5112 KB Output is correct
17 Correct 7 ms 5112 KB Output is correct
18 Correct 8 ms 5240 KB Output is correct
19 Correct 7 ms 5112 KB Output is correct
20 Correct 8 ms 5240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 4984 KB Output is correct
4 Correct 6 ms 5112 KB Output is correct
5 Correct 85 ms 11548 KB Output is correct
6 Correct 56 ms 18936 KB Output is correct
7 Correct 84 ms 16872 KB Output is correct
8 Correct 57 ms 11128 KB Output is correct
9 Correct 81 ms 15276 KB Output is correct
10 Correct 57 ms 11080 KB Output is correct
11 Correct 8 ms 5240 KB Output is correct
12 Correct 7 ms 5112 KB Output is correct
13 Correct 7 ms 5240 KB Output is correct
14 Correct 269 ms 22648 KB Output is correct
15 Correct 266 ms 22520 KB Output is correct
16 Correct 226 ms 22612 KB Output is correct
17 Correct 280 ms 22516 KB Output is correct
18 Correct 264 ms 22520 KB Output is correct
19 Correct 224 ms 22592 KB Output is correct
20 Correct 266 ms 22520 KB Output is correct
21 Correct 34 ms 6008 KB Output is correct
22 Correct 273 ms 22984 KB Output is correct
23 Correct 289 ms 22812 KB Output is correct
24 Correct 228 ms 22780 KB Output is correct
25 Correct 268 ms 22904 KB Output is correct
26 Correct 229 ms 22876 KB Output is correct
27 Correct 269 ms 22732 KB Output is correct
28 Correct 272 ms 22788 KB Output is correct
29 Correct 228 ms 22776 KB Output is correct
30 Correct 270 ms 22848 KB Output is correct
31 Correct 320 ms 14760 KB Output is correct
32 Correct 223 ms 22588 KB Output is correct
33 Correct 379 ms 20116 KB Output is correct
34 Correct 145 ms 14292 KB Output is correct
35 Correct 324 ms 19752 KB Output is correct
36 Correct 147 ms 14324 KB Output is correct
37 Correct 400 ms 19320 KB Output is correct
38 Correct 269 ms 15352 KB Output is correct
39 Correct 221 ms 22676 KB Output is correct
40 Correct 380 ms 18384 KB Output is correct
41 Correct 8 ms 5112 KB Output is correct
42 Correct 8 ms 5240 KB Output is correct
43 Correct 8 ms 5240 KB Output is correct
44 Correct 7 ms 5240 KB Output is correct
45 Correct 8 ms 5240 KB Output is correct
46 Correct 8 ms 5112 KB Output is correct
47 Correct 7 ms 5112 KB Output is correct
48 Correct 8 ms 5240 KB Output is correct
49 Correct 7 ms 5112 KB Output is correct
50 Correct 8 ms 5240 KB Output is correct
51 Correct 262 ms 15568 KB Output is correct
52 Correct 267 ms 22948 KB Output is correct
53 Correct 401 ms 18696 KB Output is correct
54 Correct 132 ms 15096 KB Output is correct
55 Correct 309 ms 15352 KB Output is correct
56 Correct 275 ms 22904 KB Output is correct
57 Correct 318 ms 19648 KB Output is correct
58 Correct 157 ms 14656 KB Output is correct
59 Correct 274 ms 15584 KB Output is correct
60 Correct 276 ms 22904 KB Output is correct
61 Correct 321 ms 19672 KB Output is correct
62 Correct 148 ms 14644 KB Output is correct
63 Correct 313 ms 15188 KB Output is correct
64 Correct 269 ms 22824 KB Output is correct
65 Correct 376 ms 19332 KB Output is correct
66 Correct 140 ms 15224 KB Output is correct
67 Correct 312 ms 15084 KB Output is correct
68 Correct 275 ms 22788 KB Output is correct
69 Correct 317 ms 18312 KB Output is correct
70 Correct 153 ms 14776 KB Output is correct