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 <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
#define FOR(i, l, r) for (int i = (l); i <= (r); ++i)
#define FOS(i, r, l) for (int i = (r); i >= (l); --i)
#define FRN(i, n) for (int i = 0; i < (n); ++i)
#define FSN(i, n) for (int i = (n) - 1; i >= 0; --i)
#define EACH(i, x) for (auto &i : (x))
#define WHILE while
template<typename T> T gcd(T a, T b) { WHILE(b) { a = a % b; swap(a, b); } return a; }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
END OF TEMPLATE
----------------------------------------------------------------
Tran The Bao - ghostwriter
Training for VOI23 gold medal
----------------------------------------------------------------
GOAT
----------------------------------------------------------------
*/
struct Plan {
int a, b, c;
Plan() {}
Plan(int a, int b, int c) : a(a), b(b), c(c) {}
};
const int N = 1e5 + 5;
int n, m, h[N], tin[N], s[N], p[N][17], d[N], f[N], timer = 0;
vi adj[N], d1[N];
Plan P[N];
void input() {
cin >> n;
FOR(i, 1, n - 1) {
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
cin >> m;
FOR(i, 1, m) cin >> P[i].a >> P[i].b >> P[i].c;
}
void dfs(int u, int p1) {
tin[u] = ++timer;
s[u] = 1;
p[u][0] = p1;
if (p1) FOR(i, 1, 16) p[u][i] = p[p[u][i - 1]][i - 1];
EACH(v, adj[u]) {
if (v == p1) continue;
h[v] = h[u] + 1;
dfs(v, u);
s[u] += s[v];
}
}
int lca(int a, int b) {
if (h[a] > h[b]) swap(a, b);
int diff = h[b] - h[a];
FRN(i, 17)
if (diff & (1 << i))
b = p[b][i];
if (a == b) return a;
FSN(i, 17)
if (p[a][i] != p[b][i]) {
a = p[a][i];
b = p[b][i];
}
return p[a][0];
}
int getn(int a, int h) { FSN(i, 17) if (h & (1 << i)) a = p[a][i]; return a; }
void upd(int pos, int v) { for (int i = pos; i <= n; i += (i & -i)) f[i] += v; }
void upd(int l, int r, int v) { upd(l, v); upd(r + 1, -v); }
int get(int pos) { int rs = 0; for (int i = pos; i > 0; i -= (i & -i)) rs += f[i]; return rs; }
void dfs1(int u, int p1) {
int sum = 0;
EACH(v, adj[u]) {
if (v == p1) continue;
dfs1(v, u);
sum += d[v];
}
d[u] = sum;
EACH(i, d1[u]) {
int a = P[i].a, b = P[i].b, c = P[i].c;
d[u] = max(d[u], get(tin[a]) + get(tin[b]) + sum + c);
}
upd(tin[u], tin[u] + s[u] - 1, sum - d[u]);
}
void solve() {
dfs(1, 0);
FOR(i, 1, m) d1[lca(P[i].a, P[i].b)].pb(i);
dfs1(1, 0);
cout << d[1];
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
input();
solve();
return 0;
}
/*
----------------------------------------------------------------
From Benq:
stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/
# | 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... |