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 <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 (stderr)
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);
~~~~~^~~~~~~~~~~~~~~~~~~
# | 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... |