#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,u,s[100005],e[100005],dep[100005],t[25][100005],d[100005];
vector<ll>g[100005];
vector<tuple<ll,ll,ll>>v[100005];
struct ain
{
ll aint[400005],lim;
void init()
{
lim=1;
while(lim<=n)
lim=lim*2;
}
void update(ll a,ll b,ll nr)
{
a+=lim;
b+=lim;
while(a<=b)
{
if (a%2==1)
aint[a++]+=nr;
if (b%2==0)
aint[b--]+=nr;
a=a/2;
b=b/2;
}
}
ll query(ll poz)
{
ll ans=0;
poz+=lim;
while(poz){
ans=ans+aint[poz];
poz=poz/2;
}
return ans;
}
}ai;
ll lca(ll x,ll y)
{
if (dep[x]>dep[y])
swap(x,y);
ll i;
for(i=20;i>=0;i--)
if (dep[y]-dep[x]>=(1<<i))
y=t[i][y];
if (x==y)
return x;
for(i=20;i>=0;i--)
if (t[i][x]!=t[i][y]){
x=t[i][x];
y=t[i][y];
}
return t[0][x];
}
void dfs(ll nod,ll ta)
{
s[nod]=++u;
dep[nod]=dep[ta]+1;
t[0][nod]=ta;
for(auto it:g[nod])
if (it!=ta)
dfs(it,nod);
e[nod]=u;
}
void solve(ll nod,ll ta)
{
ll ans=0;
for(auto it:g[nod]){
if (it==ta)
continue;
solve(it,nod);
ans=ans+d[it];
}
d[nod]=ans;
for(auto it:v[nod]){
ll a,b,c;
tie(a,b,c)=it;
d[nod]=max(d[nod],ans+ai.query(s[a])+ai.query(s[b])+c);
}
ai.update(s[nod],e[nod],ans-d[nod]);
}
int main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
ll i,x,y;
scanf("%lld",&n);
for(i=1;i<n;i++){
scanf("%lld%lld",&x,&y);
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1,0);
ll j;
for(i=1;i<20;i++)
for(j=1;j<=n;j++)
t[i][j]=t[i-1][t[i-1][j]];
ll m;
scanf("%lld",&m);
for(i=1;i<=m;i++){
ll z;
scanf("%lld%lld%lld",&x,&y,&z);
ll nod=lca(x,y);
v[nod].push_back(make_tuple(x,y,z));
}
ai.init();
solve(1,0);
printf("%lld\n",d[1]);
return 0;
}
Compilation message
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:90:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
90 | scanf("%lld",&n);
| ~~~~~^~~~~~~~~~~
election_campaign.cpp:92:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
92 | scanf("%lld%lld",&x,&y);
| ~~~~~^~~~~~~~~~~~~~~~~~
election_campaign.cpp:102:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
102 | scanf("%lld",&m);
| ~~~~~^~~~~~~~~~~
election_campaign.cpp:105:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
105 | scanf("%lld%lld%lld",&x,&y,&z);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5356 KB |
Output is correct |
5 |
Correct |
101 ms |
30444 KB |
Output is correct |
6 |
Correct |
65 ms |
37484 KB |
Output is correct |
7 |
Correct |
92 ms |
35436 KB |
Output is correct |
8 |
Correct |
73 ms |
29932 KB |
Output is correct |
9 |
Correct |
96 ms |
33772 KB |
Output is correct |
10 |
Correct |
76 ms |
29932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5484 KB |
Output is correct |
4 |
Correct |
155 ms |
39916 KB |
Output is correct |
5 |
Correct |
156 ms |
39916 KB |
Output is correct |
6 |
Correct |
138 ms |
39916 KB |
Output is correct |
7 |
Correct |
157 ms |
39916 KB |
Output is correct |
8 |
Correct |
183 ms |
39916 KB |
Output is correct |
9 |
Correct |
146 ms |
39916 KB |
Output is correct |
10 |
Correct |
155 ms |
39916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5484 KB |
Output is correct |
4 |
Correct |
155 ms |
39916 KB |
Output is correct |
5 |
Correct |
156 ms |
39916 KB |
Output is correct |
6 |
Correct |
138 ms |
39916 KB |
Output is correct |
7 |
Correct |
157 ms |
39916 KB |
Output is correct |
8 |
Correct |
183 ms |
39916 KB |
Output is correct |
9 |
Correct |
146 ms |
39916 KB |
Output is correct |
10 |
Correct |
155 ms |
39916 KB |
Output is correct |
11 |
Correct |
15 ms |
6380 KB |
Output is correct |
12 |
Correct |
157 ms |
39916 KB |
Output is correct |
13 |
Correct |
159 ms |
40044 KB |
Output is correct |
14 |
Correct |
137 ms |
39916 KB |
Output is correct |
15 |
Correct |
170 ms |
39916 KB |
Output is correct |
16 |
Correct |
141 ms |
40044 KB |
Output is correct |
17 |
Correct |
159 ms |
40044 KB |
Output is correct |
18 |
Correct |
156 ms |
40044 KB |
Output is correct |
19 |
Correct |
147 ms |
39916 KB |
Output is correct |
20 |
Correct |
161 ms |
39916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
365 ms |
32228 KB |
Output is correct |
2 |
Correct |
137 ms |
42348 KB |
Output is correct |
3 |
Correct |
352 ms |
39720 KB |
Output is correct |
4 |
Correct |
247 ms |
34592 KB |
Output is correct |
5 |
Correct |
234 ms |
39080 KB |
Output is correct |
6 |
Correct |
239 ms |
34276 KB |
Output is correct |
7 |
Correct |
357 ms |
38832 KB |
Output is correct |
8 |
Correct |
253 ms |
34796 KB |
Output is correct |
9 |
Correct |
134 ms |
42348 KB |
Output is correct |
10 |
Correct |
370 ms |
37664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5356 KB |
Output is correct |
5 |
Correct |
101 ms |
30444 KB |
Output is correct |
6 |
Correct |
65 ms |
37484 KB |
Output is correct |
7 |
Correct |
92 ms |
35436 KB |
Output is correct |
8 |
Correct |
73 ms |
29932 KB |
Output is correct |
9 |
Correct |
96 ms |
33772 KB |
Output is correct |
10 |
Correct |
76 ms |
29932 KB |
Output is correct |
11 |
Correct |
5 ms |
5484 KB |
Output is correct |
12 |
Correct |
5 ms |
5484 KB |
Output is correct |
13 |
Correct |
5 ms |
5484 KB |
Output is correct |
14 |
Correct |
5 ms |
5484 KB |
Output is correct |
15 |
Correct |
5 ms |
5484 KB |
Output is correct |
16 |
Correct |
6 ms |
5484 KB |
Output is correct |
17 |
Correct |
5 ms |
5484 KB |
Output is correct |
18 |
Correct |
5 ms |
5484 KB |
Output is correct |
19 |
Correct |
5 ms |
5484 KB |
Output is correct |
20 |
Correct |
5 ms |
5484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5356 KB |
Output is correct |
5 |
Correct |
101 ms |
30444 KB |
Output is correct |
6 |
Correct |
65 ms |
37484 KB |
Output is correct |
7 |
Correct |
92 ms |
35436 KB |
Output is correct |
8 |
Correct |
73 ms |
29932 KB |
Output is correct |
9 |
Correct |
96 ms |
33772 KB |
Output is correct |
10 |
Correct |
76 ms |
29932 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5484 KB |
Output is correct |
14 |
Correct |
155 ms |
39916 KB |
Output is correct |
15 |
Correct |
156 ms |
39916 KB |
Output is correct |
16 |
Correct |
138 ms |
39916 KB |
Output is correct |
17 |
Correct |
157 ms |
39916 KB |
Output is correct |
18 |
Correct |
183 ms |
39916 KB |
Output is correct |
19 |
Correct |
146 ms |
39916 KB |
Output is correct |
20 |
Correct |
155 ms |
39916 KB |
Output is correct |
21 |
Correct |
15 ms |
6380 KB |
Output is correct |
22 |
Correct |
157 ms |
39916 KB |
Output is correct |
23 |
Correct |
159 ms |
40044 KB |
Output is correct |
24 |
Correct |
137 ms |
39916 KB |
Output is correct |
25 |
Correct |
170 ms |
39916 KB |
Output is correct |
26 |
Correct |
141 ms |
40044 KB |
Output is correct |
27 |
Correct |
159 ms |
40044 KB |
Output is correct |
28 |
Correct |
156 ms |
40044 KB |
Output is correct |
29 |
Correct |
147 ms |
39916 KB |
Output is correct |
30 |
Correct |
161 ms |
39916 KB |
Output is correct |
31 |
Correct |
365 ms |
32228 KB |
Output is correct |
32 |
Correct |
137 ms |
42348 KB |
Output is correct |
33 |
Correct |
352 ms |
39720 KB |
Output is correct |
34 |
Correct |
247 ms |
34592 KB |
Output is correct |
35 |
Correct |
234 ms |
39080 KB |
Output is correct |
36 |
Correct |
239 ms |
34276 KB |
Output is correct |
37 |
Correct |
357 ms |
38832 KB |
Output is correct |
38 |
Correct |
253 ms |
34796 KB |
Output is correct |
39 |
Correct |
134 ms |
42348 KB |
Output is correct |
40 |
Correct |
370 ms |
37664 KB |
Output is correct |
41 |
Correct |
5 ms |
5484 KB |
Output is correct |
42 |
Correct |
5 ms |
5484 KB |
Output is correct |
43 |
Correct |
5 ms |
5484 KB |
Output is correct |
44 |
Correct |
5 ms |
5484 KB |
Output is correct |
45 |
Correct |
5 ms |
5484 KB |
Output is correct |
46 |
Correct |
6 ms |
5484 KB |
Output is correct |
47 |
Correct |
5 ms |
5484 KB |
Output is correct |
48 |
Correct |
5 ms |
5484 KB |
Output is correct |
49 |
Correct |
5 ms |
5484 KB |
Output is correct |
50 |
Correct |
5 ms |
5484 KB |
Output is correct |
51 |
Correct |
257 ms |
35240 KB |
Output is correct |
52 |
Correct |
161 ms |
42752 KB |
Output is correct |
53 |
Correct |
369 ms |
38080 KB |
Output is correct |
54 |
Correct |
177 ms |
34528 KB |
Output is correct |
55 |
Correct |
370 ms |
34976 KB |
Output is correct |
56 |
Correct |
159 ms |
42604 KB |
Output is correct |
57 |
Correct |
241 ms |
38824 KB |
Output is correct |
58 |
Correct |
246 ms |
34544 KB |
Output is correct |
59 |
Correct |
264 ms |
35044 KB |
Output is correct |
60 |
Correct |
159 ms |
42768 KB |
Output is correct |
61 |
Correct |
238 ms |
39040 KB |
Output is correct |
62 |
Correct |
258 ms |
34256 KB |
Output is correct |
63 |
Correct |
360 ms |
34704 KB |
Output is correct |
64 |
Correct |
151 ms |
42604 KB |
Output is correct |
65 |
Correct |
364 ms |
38816 KB |
Output is correct |
66 |
Correct |
174 ms |
34460 KB |
Output is correct |
67 |
Correct |
359 ms |
34908 KB |
Output is correct |
68 |
Correct |
164 ms |
42900 KB |
Output is correct |
69 |
Correct |
239 ms |
37672 KB |
Output is correct |
70 |
Correct |
256 ms |
34580 KB |
Output is correct |