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>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
struct fen{
vector<ll> tree;
int n;
fen(int _n = 0){
n = _n;
tree.resize(n + 3, 0);
}
void upd(int p, ll c){
while(p <= n){
tree[p] += c;
p += p & -p;
}
}
ll get(int p){
ll ans = 0;
while(p){
ans += tree[p];
p -= p & -p;
}
return ans;
}
} bit0, bit1;
const int MAX = 1e5 + 10, LOG = 17;
int N, M;
vector<int> adj[MAX];
int h[MAX], d[MAX][LOG], f[MAX][2], timer, in[MAX], out[MAX];
vector<array<int, 3>> plans[MAX];
void dfs(int u, int p = -1){
in[u] = ++timer;
for(int v: adj[u]){
if(v == p) continue;
h[v] = h[u] + 1;
d[v][0] = u;
for(int i = 1; i < LOG; i++) d[v][i] = d[d[v][i - 1]][i - 1];
dfs(v, u);
}
out[u] = timer;
}
int lca(int u, int v){
if(h[u] < h[v]) swap(u, v);
int delta = h[u] - h[v];
for(int i = 0; i < LOG; i++) if(BIT(delta, i)){
u = d[u][i];
}
if(u == v) return u;
for(int i = LOG - 1; i >= 0; i--) if(d[u][i] != d[v][i]){
u = d[u][i];
v = d[v][i];
}
return d[u][0];
}
void dfs2(int u, int p = -1){
for(int v: adj[u]){
if(v == p) continue;
dfs2(v, u);
f[u][0] += f[v][1];
}
bit0.upd(in[u], f[u][0]);
bit0.upd(out[u] + 1, -f[u][0]);
f[u][1] = f[u][0];
for(auto o: plans[u]){
int a = o[0], b = o[1], c = o[2];
ll sum = bit0.get(in[a]) + bit0.get(in[b]) - bit0.get(in[u]) - bit1.get(in[a]) - bit1.get(in[b]);
maximize(f[u][1], sum + c);
}
bit1.upd(in[u], f[u][1]);
bit1.upd(out[u] + 1, -f[u][1]);
}
void solve(){
cin >> N;
for(int i = 1; i < N; i++){
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1);
cin >> M;
for(int i = 1; i <= M; i++){
int u, v, c;
cin >> u >> v >> c;
int p = lca(u, v);
plans[p].push_back({u, v, c});
}
bit0 = fen(N);
bit1 = fen(N);
dfs2(1);
ll ans = 0;
for(int i = 1; i <= N; i++) maximize(ans, f[i][1]);
cout << ans << '\n';
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("order.inp","r",stdin);
// freopen("order.out","w",stdout);
solve();
return 0;
}
# | 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... |