#include <bits/stdc++.h>
#define MAXN 100007
#define MAXL 20
using namespace std;
vector<int> g[MAXN],a[MAXN],b[MAXN],c[MAXN];
int n,m,p[MAXL][MAXN],t,dp[MAXN],seg[8*MAXN],in[MAXN],out[MAXN];
void dfs(int s,int f)
{
p[0][s]=f;
in[s]=t++;
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfs(g[s][i],s);
out[s]=t++;
}
void lca_init()
{
dfs(1,1);
for(int j=1;j<MAXL;j++) for(int i=1;i<=n;i++) p[j][i]=p[j-1][p[j-1][i]];
}
bool insub(int u,int v) {return in[u]>=in[v] && out[u]<=out[v];}
int lca(int u,int v)
{
if(insub(u,v)) return v;
if(insub(v,u)) return u;
for(int j=MAXL-1;j>=0;j--) if(!insub(u,p[j][v])) v=p[j][v];
return p[0][v];
}
void upd(int l,int r,int p,int v,int ind)
{
if(l==r) {seg[ind]+=v; return;}
int s=(l+r)/2;
if(p<=s) upd(l,s,p,v,2*ind);
else upd(s+1,r,p,v,2*ind+1);
seg[ind]=seg[2*ind]+seg[2*ind+1];
}
int sum(int l,int r,int lt,int rt,int ind)
{
if(lt<=l && rt>=r) return seg[ind];
if(lt>r || l>rt) return 0;
int s=(l+r)/2;
return sum(l,s,lt,rt,2*ind)+sum(s+1,r,lt,rt,2*ind+1);
}
void dfssolve(int s,int f)
{
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfssolve(g[s][i],s);
int x=sum(0,2*n,in[s],in[s],1);
dp[s]=x;
for(int i=0;i<a[s].size();i++) dp[s]=max(dp[s],c[s][i]+sum(0,2*n,in[s],in[a[s][i]],1)+sum(0,2*n,in[s],in[b[s][i]],1)-x);
upd(0,2*n,in[f],dp[s],1);
upd(0,2*n,in[s],-dp[s],1);
upd(0,2*n,out[f],-dp[s],1);
upd(0,2*n,out[s],dp[s],1);
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n-1;i++)
{
int t1,t2;
scanf("%d%d",&t1,&t2);
g[t1].push_back(t2);
g[t2].push_back(t1);
}
lca_init();
scanf("%d",&m);
for(int i=0;i<m;i++)
{
int t1,t2,t3;
scanf("%d%d%d",&t1,&t2,&t3);
int x=lca(t1,t2);
a[x].push_back(t1);
b[x].push_back(t2);
c[x].push_back(t3);
}
dfssolve(1,1);
printf("%d",dp[1]);
}
Compilation message
election_campaign.cpp: In function 'void dfs(int, int)':
election_campaign.cpp:11:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfs(g[s][i],s);
~^~~~~~~~~~~~
election_campaign.cpp: In function 'void dfssolve(int, int)':
election_campaign.cpp:44:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfssolve(g[s][i],s);
~^~~~~~~~~~~~
election_campaign.cpp:47:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<a[s].size();i++) dp[s]=max(dp[s],c[s][i]+sum(0,2*n,in[s],in[a[s][i]],1)+sum(0,2*n,in[s],in[b[s][i]],1)-x);
~^~~~~~~~~~~~
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:55:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
election_campaign.cpp:59:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&t1,&t2);
~~~~~^~~~~~~~~~~~~~~~
election_campaign.cpp:64:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&m);
~~~~~^~~~~~~~~
election_campaign.cpp:68:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d",&t1,&t2,&t3);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9848 KB |
Output is correct |
2 |
Correct |
10 ms |
9976 KB |
Output is correct |
3 |
Correct |
10 ms |
9976 KB |
Output is correct |
4 |
Correct |
14 ms |
10068 KB |
Output is correct |
5 |
Correct |
207 ms |
25044 KB |
Output is correct |
6 |
Correct |
133 ms |
34288 KB |
Output is correct |
7 |
Correct |
188 ms |
34288 KB |
Output is correct |
8 |
Correct |
175 ms |
34288 KB |
Output is correct |
9 |
Correct |
211 ms |
34288 KB |
Output is correct |
10 |
Correct |
190 ms |
34288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
34288 KB |
Output is correct |
2 |
Correct |
12 ms |
34288 KB |
Output is correct |
3 |
Correct |
13 ms |
34288 KB |
Output is correct |
4 |
Correct |
253 ms |
37704 KB |
Output is correct |
5 |
Correct |
246 ms |
37704 KB |
Output is correct |
6 |
Correct |
240 ms |
37952 KB |
Output is correct |
7 |
Correct |
253 ms |
38084 KB |
Output is correct |
8 |
Correct |
242 ms |
38092 KB |
Output is correct |
9 |
Correct |
233 ms |
38192 KB |
Output is correct |
10 |
Correct |
256 ms |
38192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
34288 KB |
Output is correct |
2 |
Correct |
12 ms |
34288 KB |
Output is correct |
3 |
Correct |
13 ms |
34288 KB |
Output is correct |
4 |
Correct |
253 ms |
37704 KB |
Output is correct |
5 |
Correct |
246 ms |
37704 KB |
Output is correct |
6 |
Correct |
240 ms |
37952 KB |
Output is correct |
7 |
Correct |
253 ms |
38084 KB |
Output is correct |
8 |
Correct |
242 ms |
38092 KB |
Output is correct |
9 |
Correct |
233 ms |
38192 KB |
Output is correct |
10 |
Correct |
256 ms |
38192 KB |
Output is correct |
11 |
Correct |
33 ms |
38192 KB |
Output is correct |
12 |
Correct |
253 ms |
38192 KB |
Output is correct |
13 |
Correct |
293 ms |
38192 KB |
Output is correct |
14 |
Correct |
250 ms |
38240 KB |
Output is correct |
15 |
Correct |
251 ms |
38240 KB |
Output is correct |
16 |
Correct |
242 ms |
38240 KB |
Output is correct |
17 |
Correct |
251 ms |
38240 KB |
Output is correct |
18 |
Correct |
303 ms |
38240 KB |
Output is correct |
19 |
Correct |
246 ms |
38240 KB |
Output is correct |
20 |
Correct |
249 ms |
38240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
373 ms |
38240 KB |
Output is correct |
2 |
Correct |
245 ms |
38284 KB |
Output is correct |
3 |
Correct |
486 ms |
38284 KB |
Output is correct |
4 |
Correct |
353 ms |
38284 KB |
Output is correct |
5 |
Correct |
415 ms |
38284 KB |
Output is correct |
6 |
Correct |
341 ms |
38284 KB |
Output is correct |
7 |
Correct |
454 ms |
38284 KB |
Output is correct |
8 |
Correct |
421 ms |
38284 KB |
Output is correct |
9 |
Correct |
248 ms |
38284 KB |
Output is correct |
10 |
Correct |
459 ms |
38284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9848 KB |
Output is correct |
2 |
Correct |
10 ms |
9976 KB |
Output is correct |
3 |
Correct |
10 ms |
9976 KB |
Output is correct |
4 |
Correct |
14 ms |
10068 KB |
Output is correct |
5 |
Correct |
207 ms |
25044 KB |
Output is correct |
6 |
Correct |
133 ms |
34288 KB |
Output is correct |
7 |
Correct |
188 ms |
34288 KB |
Output is correct |
8 |
Correct |
175 ms |
34288 KB |
Output is correct |
9 |
Correct |
211 ms |
34288 KB |
Output is correct |
10 |
Correct |
190 ms |
34288 KB |
Output is correct |
11 |
Correct |
12 ms |
38284 KB |
Output is correct |
12 |
Correct |
12 ms |
38284 KB |
Output is correct |
13 |
Correct |
12 ms |
38284 KB |
Output is correct |
14 |
Correct |
12 ms |
38284 KB |
Output is correct |
15 |
Correct |
17 ms |
38284 KB |
Output is correct |
16 |
Correct |
13 ms |
38284 KB |
Output is correct |
17 |
Correct |
12 ms |
38284 KB |
Output is correct |
18 |
Correct |
13 ms |
38284 KB |
Output is correct |
19 |
Correct |
12 ms |
38284 KB |
Output is correct |
20 |
Correct |
12 ms |
38284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9848 KB |
Output is correct |
2 |
Correct |
10 ms |
9976 KB |
Output is correct |
3 |
Correct |
10 ms |
9976 KB |
Output is correct |
4 |
Correct |
14 ms |
10068 KB |
Output is correct |
5 |
Correct |
207 ms |
25044 KB |
Output is correct |
6 |
Correct |
133 ms |
34288 KB |
Output is correct |
7 |
Correct |
188 ms |
34288 KB |
Output is correct |
8 |
Correct |
175 ms |
34288 KB |
Output is correct |
9 |
Correct |
211 ms |
34288 KB |
Output is correct |
10 |
Correct |
190 ms |
34288 KB |
Output is correct |
11 |
Correct |
10 ms |
34288 KB |
Output is correct |
12 |
Correct |
12 ms |
34288 KB |
Output is correct |
13 |
Correct |
13 ms |
34288 KB |
Output is correct |
14 |
Correct |
253 ms |
37704 KB |
Output is correct |
15 |
Correct |
246 ms |
37704 KB |
Output is correct |
16 |
Correct |
240 ms |
37952 KB |
Output is correct |
17 |
Correct |
253 ms |
38084 KB |
Output is correct |
18 |
Correct |
242 ms |
38092 KB |
Output is correct |
19 |
Correct |
233 ms |
38192 KB |
Output is correct |
20 |
Correct |
256 ms |
38192 KB |
Output is correct |
21 |
Correct |
33 ms |
38192 KB |
Output is correct |
22 |
Correct |
253 ms |
38192 KB |
Output is correct |
23 |
Correct |
293 ms |
38192 KB |
Output is correct |
24 |
Correct |
250 ms |
38240 KB |
Output is correct |
25 |
Correct |
251 ms |
38240 KB |
Output is correct |
26 |
Correct |
242 ms |
38240 KB |
Output is correct |
27 |
Correct |
251 ms |
38240 KB |
Output is correct |
28 |
Correct |
303 ms |
38240 KB |
Output is correct |
29 |
Correct |
246 ms |
38240 KB |
Output is correct |
30 |
Correct |
249 ms |
38240 KB |
Output is correct |
31 |
Correct |
373 ms |
38240 KB |
Output is correct |
32 |
Correct |
245 ms |
38284 KB |
Output is correct |
33 |
Correct |
486 ms |
38284 KB |
Output is correct |
34 |
Correct |
353 ms |
38284 KB |
Output is correct |
35 |
Correct |
415 ms |
38284 KB |
Output is correct |
36 |
Correct |
341 ms |
38284 KB |
Output is correct |
37 |
Correct |
454 ms |
38284 KB |
Output is correct |
38 |
Correct |
421 ms |
38284 KB |
Output is correct |
39 |
Correct |
248 ms |
38284 KB |
Output is correct |
40 |
Correct |
459 ms |
38284 KB |
Output is correct |
41 |
Correct |
12 ms |
38284 KB |
Output is correct |
42 |
Correct |
12 ms |
38284 KB |
Output is correct |
43 |
Correct |
12 ms |
38284 KB |
Output is correct |
44 |
Correct |
12 ms |
38284 KB |
Output is correct |
45 |
Correct |
17 ms |
38284 KB |
Output is correct |
46 |
Correct |
13 ms |
38284 KB |
Output is correct |
47 |
Correct |
12 ms |
38284 KB |
Output is correct |
48 |
Correct |
13 ms |
38284 KB |
Output is correct |
49 |
Correct |
12 ms |
38284 KB |
Output is correct |
50 |
Correct |
12 ms |
38284 KB |
Output is correct |
51 |
Correct |
430 ms |
38284 KB |
Output is correct |
52 |
Correct |
248 ms |
38284 KB |
Output is correct |
53 |
Correct |
475 ms |
38284 KB |
Output is correct |
54 |
Correct |
320 ms |
38284 KB |
Output is correct |
55 |
Correct |
418 ms |
38284 KB |
Output is correct |
56 |
Correct |
258 ms |
38284 KB |
Output is correct |
57 |
Correct |
418 ms |
38284 KB |
Output is correct |
58 |
Correct |
368 ms |
38284 KB |
Output is correct |
59 |
Correct |
427 ms |
38284 KB |
Output is correct |
60 |
Correct |
270 ms |
38284 KB |
Output is correct |
61 |
Correct |
409 ms |
38284 KB |
Output is correct |
62 |
Correct |
387 ms |
38284 KB |
Output is correct |
63 |
Correct |
394 ms |
38284 KB |
Output is correct |
64 |
Correct |
259 ms |
38284 KB |
Output is correct |
65 |
Correct |
415 ms |
38284 KB |
Output is correct |
66 |
Correct |
313 ms |
38284 KB |
Output is correct |
67 |
Correct |
376 ms |
38284 KB |
Output is correct |
68 |
Correct |
255 ms |
38284 KB |
Output is correct |
69 |
Correct |
434 ms |
38284 KB |
Output is correct |
70 |
Correct |
372 ms |
38284 KB |
Output is correct |