This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// author : anhtun_hi , nqg
#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll, ll>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define reset(h, v) memset(h, v, sizeof h)
#define Forv(i, a) for(auto& i : a)
#define For(i, a, b) for(int i = a; i <= b; ++i)
#define Ford(i, a, b) for(int i = a; i >= b; --i)
#define c_bit(i) __builtin_popcountll(i)
#define Bit(mask, i) ((mask >> i) & 1LL)
#define onbit(mask, i) ((mask) bitor (1LL << i))
#define offbit(mask, i) ((mask) &~ (1LL << i))
using namespace std;
namespace std {
template <typename T, int D>
struct _vector : public vector <_vector <T, D - 1>> {
static_assert(D >= 1, "Dimension must be positive!");
template <typename... Args>
_vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
};
template <typename T> struct _vector <T, 1> : public vector <T> {
_vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
};
template <class A, class B> bool minimize(A &a, B b){return a > b ? a = b, true : false;}
template <class A, class B> bool maximize(A &a, B b){return a < b ? a = b, true : false;}
}
const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0}, LOG = 20, base = 311, inf = 1e9 + 5;
const int MAXN = 1e6 + 5;
const ll mod = 1e9 + 7;
const ll oo = 1e18;
//#define int long long
int n, m; vector<int> g[MAXN];
struct Data{ int a, b; ll w; };
vector<Data> qr[MAXN];
int sz[MAXN], dep[MAXN], p[MAXN][22], in[MAXN], en[MAXN];
void dfs(int u, int par) {
static int timedfs = 0;
in[u] = ++timedfs;
p[u][0] = par, sz[u] = 1;
For(k, 1, 20) p[u][k] = p[p[u][k - 1]][k - 1];
for(int v : g[u]) {
if (v == par) continue;
dep[v] = dep[u] + 1;
dfs(v, u);
sz[u] += sz[v];
}
en[u] = timedfs;
}
int getlca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
int len = dep[u] - dep[v];
For(k, 0, 20) if (Bit(len, k)) u = p[u][k];
if (u == v) return u;
Ford(k, 20, 0) if (p[u][k] != p[v][k])
u = p[u][k], v = p[v][k];
return p[u][0];
}
ll dp[MAXN];
ll bit[MAXN];
void update(int u, ll val){
while(u <= 1e5) bit[u] += val, u += u &- u;
}
void update(int l, int r, ll val){
update(l, val), update(r + 1, - val);
}
ll get(int u){
ll res = 0;
while(u) res += bit[u], u -= u &- u;
return res;
}
void solve(int u, int par){
ll sum = 0;
Forv(v, g[u]) if(v ^ par) solve(v, u), sum += dp[v];
dp[u] = sum;
for(auto [a, b, c] : qr[u]){
ll tmp1 = get(in[a]); if(a == u) tmp1 = 0;
ll tmp2 = get(in[b]); if(b == u) tmp2 = 0;
maximize(dp[u], c + sum + tmp1 + tmp2);
}
update(in[u], en[u], sum - dp[u]);
}
void Solve() {
cin >> n;
For(i, 1, n - 1){
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
cin >> m;
For(i, 1, m){
int a, b; ll w; cin >> a >> b >> w;
qr[getlca(a, b)].push_back(Data{a, b, w});
}
solve(1, 0);
ll ans = 0;
For(i, 1, n) maximize(ans, dp[i]);
cout << ans << '\n';
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(0);
if(fopen("JOI15_election_campaign.inp", "r")) {
freopen("JOI15_election_campaign.inp", "r", stdin);
freopen("JOI15_election_campaign.out", "w", stdout);
}
int t = 1;
// cin >> t;
for (int test = 1; test <= t; test++) {
Solve();
}
return 0;
}
Compilation message (stderr)
election_campaign.cpp: In function 'int32_t main()':
election_campaign.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | freopen("JOI15_election_campaign.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:118:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | freopen("JOI15_election_campaign.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... |