#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>=1;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);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Incorrect |
4 ms |
5100 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5228 KB |
Output is correct |
3 |
Correct |
4 ms |
5484 KB |
Output is correct |
4 |
Correct |
158 ms |
42348 KB |
Output is correct |
5 |
Correct |
171 ms |
42348 KB |
Output is correct |
6 |
Correct |
148 ms |
42348 KB |
Output is correct |
7 |
Correct |
157 ms |
42348 KB |
Output is correct |
8 |
Correct |
165 ms |
42348 KB |
Output is correct |
9 |
Correct |
136 ms |
42348 KB |
Output is correct |
10 |
Correct |
154 ms |
42476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5228 KB |
Output is correct |
3 |
Correct |
4 ms |
5484 KB |
Output is correct |
4 |
Correct |
158 ms |
42348 KB |
Output is correct |
5 |
Correct |
171 ms |
42348 KB |
Output is correct |
6 |
Correct |
148 ms |
42348 KB |
Output is correct |
7 |
Correct |
157 ms |
42348 KB |
Output is correct |
8 |
Correct |
165 ms |
42348 KB |
Output is correct |
9 |
Correct |
136 ms |
42348 KB |
Output is correct |
10 |
Correct |
154 ms |
42476 KB |
Output is correct |
11 |
Correct |
15 ms |
6764 KB |
Output is correct |
12 |
Correct |
159 ms |
42604 KB |
Output is correct |
13 |
Correct |
159 ms |
42604 KB |
Output is correct |
14 |
Correct |
146 ms |
42860 KB |
Output is correct |
15 |
Correct |
157 ms |
42732 KB |
Output is correct |
16 |
Correct |
138 ms |
42604 KB |
Output is correct |
17 |
Correct |
181 ms |
42604 KB |
Output is correct |
18 |
Correct |
160 ms |
42604 KB |
Output is correct |
19 |
Correct |
140 ms |
42732 KB |
Output is correct |
20 |
Correct |
161 ms |
42604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
368 ms |
34992 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Incorrect |
4 ms |
5100 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Incorrect |
4 ms |
5100 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |