#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
using namespace std;
#define PB push_back
struct que{int a,b,c;};
int n,m,k,par[100005],sz[100005],dp[100005],hv[100005],vs[100005],id[100005],hd[100005],cn[100005],bit[100005],t[100005];
vector<int>g[100005];
vector<que>q[100005];
void dfs1(int v,int p){
sz[v]=1;
for(int i=0;i<g[v].size();i++){
int u=g[v][i];
if(u!=p){
dfs1(u,v);
if(hv[v]==-1||sz[hv[v]]<sz[u])hv[v]=u;
sz[v]+=sz[u];
}
}
}
void dfs2(int v,int p,int d,int h){
par[k]=p;
dp[k]=d;
vs[k]=v;
hd[k]=h;
cn[k]=cn[h];
id[v]=k++;
if(hv[v]!=-1)dfs2(hv[v],v,d+1,h);
for(int i=0;i<g[v].size();i++){
int u=g[v][i];
if(u!=p&&u!=hv[v]){
cn[k]=id[v];
dfs2(u,v,d+1,k);
}
}
}
void add(int a,int x){
a++;
while(a<=n){
bit[a]+=x;
a+=(a&-a);
}
}
int sum(int a){
int res=0;
a++;
while(a>0){
res+=bit[a];
a-=(a&-a);
}
return res;
}
int lca(int v,int u){
while(hd[v]!=hd[u]){
if(dp[hd[v]]<dp[hd[u]])swap(u,v);
v=cn[v];
}
if(dp[v]<dp[u])swap(u,v);
return u;
}
int main(void){
scanf("%d",&n);
for(int i=0;i<n;i++)hv[i]=-1;
for(int i=0;i<n-1;i++){
int a,b;
scanf("%d%d",&a,&b);
g[--a].PB(--b);
g[b].PB(a);
}
dfs1(0,-1);
dfs2(0,-1,0,0);
for(int i=0;i<n;i++)q[i].PB(que{i,i,0});
scanf("%d",&m);
for(int i=0;i<m;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
a=id[a-1];
b=id[b-1];
int l=lca(a,b);
q[l].PB(que{a,b,c});
}
for(int i=n-1;i>=0;i--){
int e=0;
for(int j=0;j<q[i].size();j++){
int a=q[i][j].a,b=q[i][j].b,c=q[i][j].c,s=0;
while(hd[a]!=hd[i]){
s+=sum(a)-sum(hd[a]-1);
a=cn[a];
}
s+=sum(a)-sum(i-1);
while(hd[b]!=hd[i]){
s+=sum(b)-sum(hd[b]-1);
b=cn[b];
}
if(b!=i)s+=sum(b)-sum(i);
e=max(e,s+c);
}
t[i]=e;
add(i,-e);
if(par[i]!=-1)add(id[par[i]],e);
}
printf("%d\n",t[0]);
}
Compilation message
election_campaign.cpp: In function 'void dfs1(int, int)':
election_campaign.cpp:13:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[v].size();i++){
~^~~~~~~~~~~~
election_campaign.cpp: In function 'void dfs2(int, int, int, int)':
election_campaign.cpp:30:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[v].size();i++){
~^~~~~~~~~~~~
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:85:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<q[i].size();j++){
~^~~~~~~~~~~~
election_campaign.cpp:63:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
election_campaign.cpp:67:14: 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:74:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&m);
~~~~~^~~~~~~~~
election_campaign.cpp:77:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d",&a,&b,&c);
~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
5236 KB |
Output is correct |
3 |
Correct |
6 ms |
5236 KB |
Output is correct |
4 |
Correct |
6 ms |
5336 KB |
Output is correct |
5 |
Correct |
102 ms |
15620 KB |
Output is correct |
6 |
Correct |
69 ms |
21760 KB |
Output is correct |
7 |
Correct |
89 ms |
21760 KB |
Output is correct |
8 |
Correct |
76 ms |
21760 KB |
Output is correct |
9 |
Correct |
88 ms |
21760 KB |
Output is correct |
10 |
Correct |
75 ms |
21760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
21760 KB |
Output is correct |
2 |
Correct |
6 ms |
21760 KB |
Output is correct |
3 |
Correct |
8 ms |
21760 KB |
Output is correct |
4 |
Correct |
133 ms |
23580 KB |
Output is correct |
5 |
Correct |
126 ms |
23580 KB |
Output is correct |
6 |
Correct |
121 ms |
23580 KB |
Output is correct |
7 |
Correct |
138 ms |
23580 KB |
Output is correct |
8 |
Correct |
127 ms |
23664 KB |
Output is correct |
9 |
Correct |
127 ms |
23664 KB |
Output is correct |
10 |
Correct |
122 ms |
23664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
21760 KB |
Output is correct |
2 |
Correct |
6 ms |
21760 KB |
Output is correct |
3 |
Correct |
8 ms |
21760 KB |
Output is correct |
4 |
Correct |
133 ms |
23580 KB |
Output is correct |
5 |
Correct |
126 ms |
23580 KB |
Output is correct |
6 |
Correct |
121 ms |
23580 KB |
Output is correct |
7 |
Correct |
138 ms |
23580 KB |
Output is correct |
8 |
Correct |
127 ms |
23664 KB |
Output is correct |
9 |
Correct |
127 ms |
23664 KB |
Output is correct |
10 |
Correct |
122 ms |
23664 KB |
Output is correct |
11 |
Correct |
16 ms |
23664 KB |
Output is correct |
12 |
Correct |
128 ms |
23664 KB |
Output is correct |
13 |
Correct |
148 ms |
23664 KB |
Output is correct |
14 |
Correct |
135 ms |
23664 KB |
Output is correct |
15 |
Correct |
133 ms |
23664 KB |
Output is correct |
16 |
Correct |
192 ms |
23664 KB |
Output is correct |
17 |
Correct |
142 ms |
23664 KB |
Output is correct |
18 |
Correct |
149 ms |
23664 KB |
Output is correct |
19 |
Correct |
131 ms |
23664 KB |
Output is correct |
20 |
Correct |
145 ms |
23664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
23664 KB |
Output is correct |
2 |
Correct |
131 ms |
23664 KB |
Output is correct |
3 |
Correct |
177 ms |
23664 KB |
Output is correct |
4 |
Correct |
150 ms |
23664 KB |
Output is correct |
5 |
Correct |
189 ms |
23664 KB |
Output is correct |
6 |
Correct |
141 ms |
23664 KB |
Output is correct |
7 |
Correct |
167 ms |
23664 KB |
Output is correct |
8 |
Correct |
203 ms |
23664 KB |
Output is correct |
9 |
Correct |
147 ms |
23676 KB |
Output is correct |
10 |
Correct |
165 ms |
23676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
5236 KB |
Output is correct |
3 |
Correct |
6 ms |
5236 KB |
Output is correct |
4 |
Correct |
6 ms |
5336 KB |
Output is correct |
5 |
Correct |
102 ms |
15620 KB |
Output is correct |
6 |
Correct |
69 ms |
21760 KB |
Output is correct |
7 |
Correct |
89 ms |
21760 KB |
Output is correct |
8 |
Correct |
76 ms |
21760 KB |
Output is correct |
9 |
Correct |
88 ms |
21760 KB |
Output is correct |
10 |
Correct |
75 ms |
21760 KB |
Output is correct |
11 |
Correct |
7 ms |
23676 KB |
Output is correct |
12 |
Correct |
7 ms |
23676 KB |
Output is correct |
13 |
Correct |
7 ms |
23676 KB |
Output is correct |
14 |
Correct |
7 ms |
23676 KB |
Output is correct |
15 |
Correct |
8 ms |
23676 KB |
Output is correct |
16 |
Correct |
7 ms |
23676 KB |
Output is correct |
17 |
Correct |
7 ms |
23676 KB |
Output is correct |
18 |
Correct |
7 ms |
23676 KB |
Output is correct |
19 |
Correct |
7 ms |
23676 KB |
Output is correct |
20 |
Correct |
7 ms |
23676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
5236 KB |
Output is correct |
3 |
Correct |
6 ms |
5236 KB |
Output is correct |
4 |
Correct |
6 ms |
5336 KB |
Output is correct |
5 |
Correct |
102 ms |
15620 KB |
Output is correct |
6 |
Correct |
69 ms |
21760 KB |
Output is correct |
7 |
Correct |
89 ms |
21760 KB |
Output is correct |
8 |
Correct |
76 ms |
21760 KB |
Output is correct |
9 |
Correct |
88 ms |
21760 KB |
Output is correct |
10 |
Correct |
75 ms |
21760 KB |
Output is correct |
11 |
Correct |
6 ms |
21760 KB |
Output is correct |
12 |
Correct |
6 ms |
21760 KB |
Output is correct |
13 |
Correct |
8 ms |
21760 KB |
Output is correct |
14 |
Correct |
133 ms |
23580 KB |
Output is correct |
15 |
Correct |
126 ms |
23580 KB |
Output is correct |
16 |
Correct |
121 ms |
23580 KB |
Output is correct |
17 |
Correct |
138 ms |
23580 KB |
Output is correct |
18 |
Correct |
127 ms |
23664 KB |
Output is correct |
19 |
Correct |
127 ms |
23664 KB |
Output is correct |
20 |
Correct |
122 ms |
23664 KB |
Output is correct |
21 |
Correct |
16 ms |
23664 KB |
Output is correct |
22 |
Correct |
128 ms |
23664 KB |
Output is correct |
23 |
Correct |
148 ms |
23664 KB |
Output is correct |
24 |
Correct |
135 ms |
23664 KB |
Output is correct |
25 |
Correct |
133 ms |
23664 KB |
Output is correct |
26 |
Correct |
192 ms |
23664 KB |
Output is correct |
27 |
Correct |
142 ms |
23664 KB |
Output is correct |
28 |
Correct |
149 ms |
23664 KB |
Output is correct |
29 |
Correct |
131 ms |
23664 KB |
Output is correct |
30 |
Correct |
145 ms |
23664 KB |
Output is correct |
31 |
Correct |
194 ms |
23664 KB |
Output is correct |
32 |
Correct |
131 ms |
23664 KB |
Output is correct |
33 |
Correct |
177 ms |
23664 KB |
Output is correct |
34 |
Correct |
150 ms |
23664 KB |
Output is correct |
35 |
Correct |
189 ms |
23664 KB |
Output is correct |
36 |
Correct |
141 ms |
23664 KB |
Output is correct |
37 |
Correct |
167 ms |
23664 KB |
Output is correct |
38 |
Correct |
203 ms |
23664 KB |
Output is correct |
39 |
Correct |
147 ms |
23676 KB |
Output is correct |
40 |
Correct |
165 ms |
23676 KB |
Output is correct |
41 |
Correct |
7 ms |
23676 KB |
Output is correct |
42 |
Correct |
7 ms |
23676 KB |
Output is correct |
43 |
Correct |
7 ms |
23676 KB |
Output is correct |
44 |
Correct |
7 ms |
23676 KB |
Output is correct |
45 |
Correct |
8 ms |
23676 KB |
Output is correct |
46 |
Correct |
7 ms |
23676 KB |
Output is correct |
47 |
Correct |
7 ms |
23676 KB |
Output is correct |
48 |
Correct |
7 ms |
23676 KB |
Output is correct |
49 |
Correct |
7 ms |
23676 KB |
Output is correct |
50 |
Correct |
7 ms |
23676 KB |
Output is correct |
51 |
Correct |
171 ms |
23676 KB |
Output is correct |
52 |
Correct |
120 ms |
23676 KB |
Output is correct |
53 |
Correct |
207 ms |
23676 KB |
Output is correct |
54 |
Correct |
128 ms |
23676 KB |
Output is correct |
55 |
Correct |
196 ms |
23676 KB |
Output is correct |
56 |
Correct |
139 ms |
23676 KB |
Output is correct |
57 |
Correct |
158 ms |
23676 KB |
Output is correct |
58 |
Correct |
148 ms |
23676 KB |
Output is correct |
59 |
Correct |
182 ms |
23676 KB |
Output is correct |
60 |
Correct |
129 ms |
23676 KB |
Output is correct |
61 |
Correct |
176 ms |
23676 KB |
Output is correct |
62 |
Correct |
130 ms |
23676 KB |
Output is correct |
63 |
Correct |
172 ms |
23676 KB |
Output is correct |
64 |
Correct |
126 ms |
23676 KB |
Output is correct |
65 |
Correct |
153 ms |
23676 KB |
Output is correct |
66 |
Correct |
124 ms |
23676 KB |
Output is correct |
67 |
Correct |
189 ms |
23676 KB |
Output is correct |
68 |
Correct |
131 ms |
23676 KB |
Output is correct |
69 |
Correct |
166 ms |
23676 KB |
Output is correct |
70 |
Correct |
131 ms |
23676 KB |
Output is correct |