#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 1; (i) <= (n); ++i)
#define forn(i, l, r) for(int i = (l); i <= (r); ++i)
#define ford(i, r, l) for(int i = (r); i >= (l); --i)
#define FOR(i, n) for(int i = 0; i < (n); ++i)
#define FORD(i, n) for(int i = ((n) - 1); i >= 0; --i)
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define endl "\n"
#define task "FFILL"
#define sz(a) int(a.size())
#define C(x, y) make_pair(x, y)
#define all(a) (a).begin(), (a).end()
#define bit(i, mask) (mask >> i & 1)
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
const int N = 1e5 + 3;
const int LIM = (1 << 16) + 3;
const ll INF = 1e18 + 1;
const int LOG = 16;
int n, q;
vector<int> g[N];
vector<array<int, 3>> f[N];
int tin[N], tout[N], up[N][LOG + 1], timedfs = 0;
int val[N];
int dp[N];
int bit[N];
void update(int u, int v)
{
for(; u <= n; u += u & -u) bit[u] += v;
}
int get(int u)
{
int res = 0;
for(; u; u -= u & -u) res += bit[u];
return res;
}
void pre_dfs(int u, int p = 1)
{
up[u][0] = p;
tin[u] = ++timedfs;
forn(i, 1, LOG) up[u][i] = up[up[u][i - 1]][i - 1];
for(int v : g[u]) if(v != p) pre_dfs(v, u);
tout[u] = timedfs;
}
bool isAnc(int u, int v) {return tin[u] <= tin[v] && tout[v] <= tout[u];}
int lca(int u, int v)
{
if(isAnc(u, v)) return u;
if(isAnc(v, u)) return v;
ford(i, LOG, 0) if(!isAnc(up[u][i], v)) u = up[u][i];
return up[u][0];
}
void dfs(int u, int p = 1)
{
int sum = 0, best = 0;
for(int v : g[u]) if(v != p) dfs(v, u), sum += dp[v];
for(auto &[x, y, c] : f[u])
{
int val = c + (x == u ? 0 : get(tin[x]))
+ (y == u ? 0 : get(tin[y]));
maximize(best, val);
}
dp[u] = sum + best;
update(tin[u], -best); update(tout[u] + 1, best);
}
void solve()
{
n = readInt();
rep(i, n - 1)
{
int u, v;
u = readInt();
v = readInt();
g[u].pb(v);
g[v].pb(u);
}
pre_dfs(1);
q = readInt();
rep(i, q)
{
int u, v, c;
u = readInt();
v = readInt();
c = readInt();
f[lca(u, v)].pb({u, v, c});
}
dfs(1);
cout << dp[1];
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int TC = 1;
if(fopen("note.inp", "r"))
{
freopen("note.inp", "r", stdin);
freopen("note.out", "w", stdout);
}
while(TC--)
{
solve();
}
return 0;
}
Compilation message
election_campaign.cpp: In function 'void solve()':
election_campaign.cpp:87:9: error: 'readInt' was not declared in this scope
87 | n = readInt();
| ^~~~~~~
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:120:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
120 | freopen("note.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | freopen("note.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~