#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define pb push_back
#define ppb pop_back
#define fr first
#define sc second
#define all(v) v.begin(), v.end()
#define vektor vector
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
constexpr ull Mod = 1e9 + 7;
constexpr ull sqr = 223;
constexpr ld eps = 1e-9;
int n, m;
vector <int> graph[100005];
int h[100005], p[100005][20];
void dfs1(int node, int pr){
p[node][0] = pr;
h[node] = h[pr] + 1;
for(int i = 1; i <= __lg(n); i ++) if(p[node][i - 1] != - 1) p[node][i] = p[p[node][i - 1]][i - 1];
for(auto& x : graph[node]) if(x != pr) dfs1(x, node);
}
int lca(int u, int v){
if(h[u] < h[v]) swap(u, v);
while(h[u] > h[v]) u = p[u][__lg(h[u] - h[v])];
for(int i = __lg(n); i >= 0; i --) if(p[u][i] != p[v][i]){
u = p[u][i];
v = p[v][i];
}
while(u != v){
u = p[u][0];
v = p[v][0];
break;
}
return u;
}
vector <int> ed1[100005];
int top[100005];
ll dp[100005], val[100005];
pair <int, int> sss[100005];
int childof(int u, int pr){
while(h[u] > h[pr] + 1){
u = p[u][__lg(h[u] - h[pr] - 1)];
}
return u;
}
void dfs(int node, int pr){
ll ans = 0;
for(auto& x : graph[node]){
if(x == pr) continue;
dfs(x, node);
ans += dp[x];
}
dp[node] = max(dp[node], ans);
for(auto& x : ed1[node]) {
val[x] += ans;
if(top[x] == node){
int xx = childof(sss[x].fr, node), y = - 1;
val[x] -= dp[xx];
if(sss[x].sc != node){
y = childof(sss[x].sc, node);
val[x] -= dp[y];
}
}
dp[top[x]] = max(dp[top[x]], val[x]);
}
// cout << node << " " << dp[node] << "\n";
}
void solve(){
memset(p, -1, sizeof(p));
memset(top, 0, sizeof(top));
memset(dp, 0, sizeof(dp));
cin >> n;
for(int i = 1; i < n; i ++){
int u, v;
cin >> u >> v;
graph[u].pb(v);
graph[v].pb(u);
}
dfs1(1, 0);
int timer = 0;
cin >> m;
while(m --){
ll u, v, vl;
cin >> u >> v >> vl;
timer ++;
if(h[u] < h[v]) swap(u, v);
sss[timer] = {u, v};
val[timer] = vl;
top[timer] = lca(u, v);
dp[top[timer]] = max(dp[top[timer]], vl);
ed1[u].pb(timer);
ed1[v].pb(timer);
if(v != top[timer]) ed1[top[timer]].pb(timer);
}
// for(int i = 1; i <= n; i ++) cout << top[i] << ' '; return;
dfs(1, 0);
cout << dp[1];
// for(int i = 1; i <= n; i ++) cout << dp[i] << "\n";
// for(int i = 1; i <= n; i ++){
// for(auto& x : ed1[i]) cout << x << " "; cout << "\n";
// }
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
if(fopen("nameholder.inp", "r")){
freopen("nameholder.inp", "r", stdin);
freopen("nameholder.out", "w", stdout);
}
int t = 1;
// cin >> t;
while(t --){
solve();
cout << "\n";
}
cerr << "Code time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
return 0;
}
// Whose eyes are those eyes?
컴파일 시 표준 에러 (stderr) 메시지
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:160:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
160 | freopen("nameholder.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:161:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
161 | freopen("nameholder.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |