This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |