#include <bits/stdc++.h>
#define Y8o "election_campaign"
#define maxn (int) 1e5 + 5
#define ll long long
#define pii pair<ll, ll>
#define gb(i, j) ((i >> j) & 1)
#define all(x) x.begin(), x.end()
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define fi(i, a, b) for(int i = a; i <= b; i ++)
#define fid(i, a, b) for(int i = a; i >= b; i --)
//#define f first
//#define s second
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
return uniform_int_distribution<ll> (l, r) (rng);
}
void iof() {
ios_base::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
if(fopen(Y8o".inp", "r"))
{
freopen(Y8o".inp", "r", stdin);
// freopen(Y8o".out", "w", stdout);
}
}
void ctime() {
cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
int n, Q;
vector<int> o[maxn];
int h[maxn], cha[maxn][17];
int in[maxn], out[maxn], cnt;
struct dl { int u, v, w; }; vector<dl> qr[maxn];
ll dp[maxn][2];
void pre_dfs(int u = 1, int p = 0) {
in[u] = ++cnt;
for(int v : o[u]) if(v != p) {
h[v] = h[u] + 1, cha[v][0] = u;
fi(i, 1, 16) cha[v][i] = cha[ cha[v][i - 1] ][i - 1];
pre_dfs(v, u);
}
out[u] = cnt;
}
int lca(int u, int v) {
if(h[u] < h[v]) swap(u, v);
int kc = h[u] - h[v];
fid(i, 16, 0) if(gb(kc, i)) u = cha[u][i];
fid(i, 16, 0) if(cha[u][i] != cha[v][i]) u = cha[u][i], v = cha[v][i];
return u == v ? u : cha[u][0];
}
struct BIT {
ll bit[maxn];
void update(int l, int r, ll w) {
r ++;
for(; l < maxn; l += (l & -l)) bit[l] += w;
for(; r < maxn; r += (r & -r)) bit[r] -= w;
}
ll get(int x, ll sum = 0) {
while(x > 0) sum += bit[x], x -= (x & -x);
return sum;
}
} Bit[2];
void dfs(int u = 1, int p = 0) {
for(int v : o[u]) if(v != p) {
dfs(v, u);
dp[u][0] += max(dp[v][0], dp[v][1]);
}
Bit[0].update(in[u], out[u], dp[u][0]);
for(dl x : qr[u]) {
int u1 = x.u, v1 = x.v, w = x.w;
if(h[u1] > h[v1]) swap(u1, v1);
ll val1 = Bit[0].get(in[u1]) - Bit[1].get(in[u1]), val2 = Bit[0].get(in[v1]) - Bit[1].get(in[v1]);
if(u1 == u) {
dp[u][1] = max(dp[u][1], val2 + w);
}
else {
dp[u][1] = max(dp[u][1], val1 + val2 + w - dp[u][0]);
}
}
Bit[1].update(in[u], out[u], max(dp[u][0], dp[u][1]));
}
void solve() {
cin >> n ;
fi(i, 1, n - 1) {
int u, v; cin >> u >> v;
o[u].push_back(v), o[v].push_back(u);
}
pre_dfs();
cin >> Q;
fi(i, 1, Q) {
int u, v, w; cin >> u >> v >> w;
int p = lca(u, v);
qr[p].push_back({ u, v, w });
}
dfs();
// fi(i, 1, n) cout << i << ' ' << dp[i][0] << ' ' << dp[i][1] << '\n';
cout << max(dp[1][0], dp[1][1]);
}
int main() {
iof();
int nTest = 1;
// cin >> nTest;
while(nTest --) {
solve();
}
ctime();
return 0;
}
Compilation message (stderr)
election_campaign.cpp: In function 'void iof()':
election_campaign.cpp:27:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen(Y8o".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |