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;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = b; i >= a; i --)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPD(i, n) for (int i = n - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void setupIO(){
#define name "Whisper"
//Phu Trong from Nguyen Tat Thanh High School for gifted student
srand(time(NULL));
cin.tie(nullptr)->sync_with_stdio(false); cout.tie(nullptr);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
cout << fixed << setprecision(10);
}
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
const int MAX = 1e5 + 5;
int numNode, numQuery;
struct Edge{
int u, v;
Edge(){}
Edge(int _u, int _v): u(_u), v(_v){}
int other(int x){ return (x ^ u ^ v); }
} edge[MAX];
vector<int> G[MAX];
int tin[MAX], tout[MAX], cnt = 0;
int up[MAX][21], depth[MAX], par[MAX];
void pre_dfs(int u, int p = -1){
tin[u] = ++cnt;
for (int &i : G[u]){
int v = edge[i].other(u);
if(v ^ p){
depth[v] = depth[u] + 1;
par[v] = u;
up[v][0] = u;
for (int i = 1; i <= 20; ++i) up[v][i] = up[up[v][i - 1]][i - 1];
pre_dfs(v, u);
}
}
tout[u] = cnt;
}
int lca(int u, int v){
if (depth[u] < depth[v]) swap(u, v);
int dis = depth[u] - depth[v];
for (int i = 0; MASK(i) <= dis; ++i) if(BIT(dis, i))
u = up[u][i];
if (u == v) return u;
int k = 31 - __builtin_clz(depth[v]);
for (int i = k; i >= 0; --i) if(up[u][i] != up[v][i]){
u = up[u][i], v = up[v][i];
}
return up[u][0];
}
vector<tuple<int, int, int>> q[MAX];
int ndp[MAX], dp[MAX];
struct FenwickTree{
int n;
vector<int> f;
FenwickTree(int _n = 0){
this -> n = _n;
f.resize(n + 5);
}
void modify(int p, int val){
for (; p <= n; p += p & (-p)) f[p] += val;
}
int query(int p){
int res = 0;
for (; p > 0; p -= p & (-p)) res += f[p];
return res;
}
void modify(int l, int r, int val){
modify(l, val); modify(r + 1, -val);
}
} fen;
void dfs(int u, int p = -1){
for (int &i : G[u]){
int v = edge[i].other(u);
if(v ^ p){
dfs(v, u);
ndp[u] += dp[v];
}
}
dp[u] = ndp[u]; // not choose any segment
for (tuple<int, int, int>&x : q[u]){
int nodeu, nodev, c; tie(nodeu, nodev, c) = x;
maximize(dp[u], fen.query(tin[nodeu]) + fen.query(tin[nodev]) + c + ndp[u]);
}
fen.modify(tin[u], tout[u], ndp[u] - dp[u]);
}
void Whisper(){
cin >> numNode;
for (int i = 1; i < numNode; ++i){
cin >> edge[i].u >> edge[i].v;
G[edge[i].u].emplace_back(i);
G[edge[i].v].emplace_back(i);
}
//dp on tree
pre_dfs(1);
cin >> numQuery;
for (int i = 1; i <= numQuery; ++i){
int u, v, c; cin >> u >> v >> c;
int p = lca(u, v);
q[p].emplace_back(u, v, c);
}
fen = FenwickTree (numNode);
dfs(1);
cout << dp[1];
}
signed main(){
setupIO();
int Test = 1;
// cin >> Test;
for ( int i = 1 ; i <= Test ; i++ ){
Whisper();
if (i < Test) cout << '\n';
}
}
# | 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... |