#include <bits/stdc++.h>
#define tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0)
#define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define db long double
#define sz(a) ((int)(a).size())
#define pb emplace_back
#define pf emplace_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define ins emplace
#define mp make_pair
using namespace std;
const int MOD = 1e9+7, MAXN = 1e5+5;
const string NAME = "";
int n,m,x,y,z,bit[MAXN],dp[MAXN],child[MAXN];
int par[MAXN],depth[MAXN],sz[MAXN],head[MAXN],pos[MAXN],curpos=0;
vector<int> adj[MAXN],vertices[MAXN];
vector<pair<pair<int,int>,int>> g[MAXN];
//BIT
void update(int pos, int val){
while(pos<=n) bit[pos]+=val, pos+=pos&-pos;
}
int getsum(int pos){
int rs=0;
while(pos>0) rs+=bit[pos], pos-=pos&-pos;
return rs;
}
//HLD
void dfs(int u){
vertices[depth[u]].pb(u), sz[u]=1;
for(int v : adj[u])
if(v!=par[u]) par[v]=u, depth[v]=depth[u]+1, dfs(v), sz[u]+=sz[v];
}
void hld(int u, int curhead){
head[u]=curhead, pos[u]=++curpos;
int heavy=-1;
for(int v : adj[u])
if(v!=par[u]&&(heavy==-1||sz[v]>sz[heavy])) heavy=v;
if(heavy!=-1) hld(heavy,curhead);
for(int v : adj[u])
if(v!=par[u]&&v!=heavy) hld(v,v);
}
int LCA(int x, int y){
while(head[x]!=head[y]){
if(depth[head[x]]<depth[head[y]]) swap(x,y);
x=par[head[x]];
}
if(depth[x]<=depth[y]) return x;
return y;
}
int getpath(int x, int y){
int rs=0;
while(head[x]!=head[y]){
if(depth[head[x]]<depth[head[y]]) swap(x,y);
rs+=getsum(pos[x])-getsum(pos[head[x]]-1);
x=par[head[x]];
}
if(depth[x]<depth[y]) swap(x,y);
rs+=getsum(pos[x])-getsum(pos[y]);
return rs;
}
int main()
{
tt;
if(fopen((NAME + ".INP").c_str(), "r")) fo;
cin >> n;
for(int i = 1; i<n; ++i)
cin >> x >> y, adj[x].pb(y), adj[y].pb(x);
dfs(1);
hld(1,1);
cin >> m;
for(int i = 1; i<=m; ++i){
cin >> x >> y >> z;
if(depth[x]<depth[y]) swap(x,y);
g[LCA(x,y)].pb(mp(x,y),z);
}
for(int i = n-1; i>=0; --i)
for(int u : vertices[i]){
for(int v : adj[u])
if(v!=par[u]) child[u]+=dp[v];
dp[u]=child[u];
for(pair<pair<int,int>,int> p : g[u])
dp[u]=max(dp[u],child[u]+getpath(p.fi.fi,p.fi.se)+p.se);
update(pos[u],child[u]-dp[u]);
}
cout << dp[1];
}
Compilation message (stderr)
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:3:19: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:72:45: note: in expansion of macro 'fo'
72 | if(fopen((NAME + ".INP").c_str(), "r")) fo;
| ^~
election_campaign.cpp:3:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:72:45: note: in expansion of macro 'fo'
72 | if(fopen((NAME + ".INP").c_str(), "r")) fo;
| ^~
# | 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... |