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 MIKU
string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m";
#define debug(x...) cout << dbmc << "[" << #x << "]: ", dout(x)
void dout() { cout << dbrs << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif
#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
using ll = long long;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
const int MXN = 100005;
int n, m, a[MXN], b[MXN], c[MXN];
vector<int> lenid[MXN];
int lca[MXN], dfn[MXN];
ll dp[MXN];
struct BIT {
int n;
ll val[MXN];
void init(int _n) {
n = _n;
fill(val + 1, val + n + 1, 0);
}
void modify(int id, int v) {
for (; id <= n; id += (id & -id)) val[id] += v;
}
ll query(int id) {
ll ans = 0;
for (; id > 0; id -= (id & -id)) ans += val[id];
return ans;
}
ll query(int l, int r) {
return query(r) - query(l - 1);
}
} B;
namespace TREE {
int p[MXN], d[MXN], sz[MXN], son[MXN], top[MXN], C = 1;
vector<int> edge[MXN];
void ADD_EDGE(int x, int y) {
edge[x].push_back(y);
edge[y].push_back(x);
}
void DFS(int id, int par, int dep) {
p[id] = par;
d[id] = dep;
sz[id] = 1;
son[id] = -1;
for (auto &i : edge[id]) {
if (i == par) continue;
DFS(i, id, dep + 1);
sz[id] += sz[i];
if (son[id] == -1 || sz[son[id]] < sz[i]) son[id] = i;
}
}
void HLD(int id, int par, int _t) {
top[id] = _t;
dfn[id] = C++;
if (son[id] != -1) HLD(son[id], id, _t);
for (auto &i : edge[id]) {
if (i == par) continue;
if (i == son[id]) continue;
HLD(i, id, i);
}
}
void BUILD() {
DFS(1, 0, 0);
HLD(1, 0, 1);
}
int LCA(int x, int y) {
while (top[x] != top[y]) {
if (d[top[x]] < d[top[y]]) swap(x, y);
x = p[top[x]];
}
return (d[x] > d[y] ? y : x);
}
}
ll JUMP(int rt, int x) {
using namespace TREE;
ll ans = 0;
while (top[x] != top[rt]) {
ans += B.query(dfn[top[x]], dfn[x]);
x = p[top[x]];
}
return ans + B.query(dfn[rt] + 1, dfn[x]);
}
void DP(int id, int par) {
ll sum = 0;
for (auto &i : TREE::edge[id]) {
if (i == par) continue;
DP(i, id);
sum += dp[i];
}
dp[id] = sum;
for (auto &e : lenid[id]) {
debug(id, e, a[e], b[e]);
int l = JUMP(id, a[e]), r = JUMP(id, b[e]);
ll now = sum + c[e] - l - r;
debug(l, r, now);
dp[id] = max(dp[id], now);
}
B.modify(dfn[id], dp[id] - sum);
debug(id, dp[id], dp[id] - sum);
}
void miku() {
int x, y;
cin >> n;
FOR(i, 1, n) {
cin >> x >> y;
TREE::ADD_EDGE(x, y);
}
TREE::BUILD();
cin >> m;
FOR(i, 0, m) {
cin >> a[i] >> b[i] >> c[i];
lca[i] = TREE::LCA(a[i], b[i]);
lenid[lca[i]].push_back(i);
}
B.init(n);
DP(1, 0);
cout << dp[1] << '\n';
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(false);
cin.exceptions(cin.failbit);
miku();
return 0;
}
Compilation message (stderr)
election_campaign.cpp: In function 'void DP(int, int)':
election_campaign.cpp:11:20: warning: statement has no effect [-Wunused-value]
11 | #define debug(...) 39
| ^~
election_campaign.cpp:111:3: note: in expansion of macro 'debug'
111 | debug(id, e, a[e], b[e]);
| ^~~~~
election_campaign.cpp:11:20: warning: statement has no effect [-Wunused-value]
11 | #define debug(...) 39
| ^~
election_campaign.cpp:114:3: note: in expansion of macro 'debug'
114 | debug(l, r, now);
| ^~~~~
election_campaign.cpp:11:20: warning: statement has no effect [-Wunused-value]
11 | #define debug(...) 39
| ^~
election_campaign.cpp:118:2: note: in expansion of macro 'debug'
118 | debug(id, dp[id], dp[id] - sum);
| ^~~~~
# | 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... |