#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double dou;
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define MASK(x) (1LL<<(x))
#define BITj(x, j) (((x)>>(j))&1)
#define ALL(v) (v).begin(), (v).end()
template<typename T> bool maximize(T &x, const T &y) {
if (x < y) {x = y; return 1;}
return 0;
}
template<typename T> bool minimize(T &x, const T &y) {
if (x > y) {x = y; return 1;}
return 0;
}
struct BIT{
int n;
vector<int> bit;
void init(int _n = 0) {
n = _n;
bit.assign(n+1, 0);
}
void upd(int k, int x) {
while (k <= n) {
bit[k] += x;
k += k & (-k);
}
}
void update(int l, int r, int x) {
upd(l, x);
upd(r+1, -x);
}
int query(int k) {
int res = 0;
while (k > 0) {
res += bit[k];
k -= k & (-k);
}
return res;
}
};
struct path{
int u, v, c;
path(int _u, int _v, int _c) {
u = _u;
v = _v;
c = _c;
}
};
const int maxn = 1e5+5, lg = 17;
int n, q;
vector<int> a[maxn];
vector<path> qu[maxn];
int tin[maxn], tout[maxn], par[maxn][lg], h[maxn], timer = 0;
int dp[maxn][2], pos[maxn];
BIT bit;
void dfspre(int u, int p) {
tin[u] = ++timer;
for (int j = 1; j < lg; j++) {
if (h[u] <= MASK(j)) break;
par[u][j] = par[par[u][j-1]][j-1];
}
for (int v : a[u]) {
if (v == p) continue;
par[v][0] = u;
h[v] = h[u]+1;
dfspre(v, u);
}
tout[u] = timer;
}
bool inside(int u, int v) {
return (tin[v] <= tin[u] && tout[u] <= tout[v]);
}
int binlift(int u, int v) {
for (int j = lg-1; j >= 0; j--) {
while (h[u] > MASK(j) && !inside(v, par[u][j])) u = par[u][j];
}
return u;
}
int lca(int u, int v) {
if (inside(u, v)) return v;
if (inside(v, u)) return u;
for (int j = lg-1; j >= 0; j--) {
while (h[u] > MASK(j) && !inside(v, par[u][j])) u = par[u][j];
}
return par[u][0];
}
void dfssolve(int u, int p) {
dp[u][0] = dp[u][1] = 0;
int sz = a[u].size();
// int ps[sz+1];
// ps[0] = 0;
for (int i = 1; i <= sz; i++) {
int v = a[u][i-1];
// ps[i] = ps[i-1];
if (v == p) continue;
dfssolve(v, u);
pos[v] = i;
dp[u][0] += max(dp[v][0], dp[v][1]);
// ps[i] += max(dp[v][0], dp[v][1]);
}
// auto query = [&](int l, int r) {
// l = max(1, l);
// r = min(r, sz);
// if (l > r) return 0;
// return ps[r]-ps[l-1];
// };
//1
for (path e : qu[u]) {
int v1 = e.u, v2 = e.v, c = e.c;
if (v1 == u) {
// int p = pos[binlift(v2, u)];
// int val = c+dp[v2][0]+query(1, p-1)+query(p+1, sz);
// cout << u << ' ' << v2 << ' ' << c << ' ' << dp[u][0] << ' ' << bit.query(tin[v2]) << "\n";
int val = c+dp[u][0]+bit.query(tin[v2]);
maximize(dp[u][1], val);
}
else {
// int l = pos[binlift(v1, u)], r = pos[binlift(v2, u)];
// int val = c+dp[v1][0]+dp[v2][0]+query(1, l-1)+query(l+1, r-1)+query(r+1, sz);
int val = c+dp[u][0]+bit.query(tin[v1])+bit.query(tin[v2]);
maximize(dp[u][1], val);
}
}
//update
bit.update(tin[u], tout[u], dp[u][0]-max(dp[u][0], dp[u][1]));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#define name "task"
// if (fopen(name".inp", "r")) {
// freopen(name".inp", "r", stdin);
// freopen(name".out", "w", stdout);
// }
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
a[u].pb(v);
a[v].pb(u);
}
h[1] = 1;
dfspre(1, -1);
bit.init(n);
cin >> q;
while (q--) {
int u, v, c;
cin >> u >> v >> c;
if (tin[u] > tin[v]) swap(u, v);
qu[lca(u, v)].pb(path(u, v, c));
}
dfssolve(1, -1);
// for (int i = 1; i <= n; i++) {
// cout << i << ' ' << dp[i][0] << ' ' << dp[i][1] << "\n";
// }
cout << max(dp[1][0], dp[1][1]);
}
/*
6
1 2
2 3
2 4
3 5
5 6
5
5 1 5
6 5 9
5 6 6
3 4 3
4 1 6
15
7
3 4
6 5
2 7
1 5
7 5
4 5
5
4 3 10
5 6 5
2 6 9
7 2 2
1 3 8
*/
# | 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... |