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;
typedef long long ll;
#define int ll
typedef pair<int, int> pi;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define maxn 200010
#define INF (int)1e9
#define MOD 998244353
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
#define DEBUG 0
int TC,N,K;
int depth[maxn],twok[maxn][20],st[maxn],en[maxn];
vector <pii> A[maxn];
int M;
int a,b,c;
vi adj[maxn];
int co = 1;
struct FW{
int fw[maxn],N;
FW(int _N){
N = _N; FOR(i,1,N) fw[i] = 0;
}
void update(int p,int v){
for (int i=p;i<=N;i+=i&(-i)) fw[i] += v;
}
void upd(int a,int b,int c){
update(a,c); update(b+1,-c);
}
int qry(int p){
int ans = 0;
for (int i=p;i>0;i-=i&(-i)) ans += fw[i];
return ans;
}
}*fw;
void dfs(int x,int p){
twok[x][0] = p;
st[x] = co++;
aFOR(i, adj[x]) if (i != p){
depth[i] = depth[x] + 1; dfs(i,x);
}
en[x] = co - 1;
}
int kpar(int x,int k){
FOR(i,0,19) if (k &(1<<i)) x = twok[x][k];
return x;
}
int lca(int a,int b){
if (depth[a] < depth[b]) swap(a,b);
int d = depth[a] - depth[b];
FOR(i,0,19) if ((1<<i)&d) a = twok[a][i];
if (a == b) return a;
DEC(i,19,0) if (twok[a][i] != twok[b][i]){
a = twok[a][i]; b = twok[b][i];
}
return twok[a][0];
}
int dp[maxn];
void compdp(int x,int p){
aFOR(i,adj[x]) if (i != p) compdp(i,x);
int sm = 0;
aFOR(i,adj[x]) if (i != p) sm += dp[i];
dp[x] = sm;
aFOR(i,A[x]){
int a = i.f.f, b = i.f.s, c = i.s;
int res = c + sm;
if (a != x){
int ap = kpar(a,depth[a]-depth[x]-1);
res += fw->qry(st[a]) - fw->qry(st[x]);
}
if (b != x){
int bp = kpar(b,depth[b]-depth[x]-1);
res += fw->qry(st[b]) - fw->qry(st[x]);
}
dp[x] = max(dp[x], res);
}
fw->upd(st[x], en[x], sm - dp[x]);
}
int32_t main(){
fast;
cin >> N;
FOR(i,1,N-1){
cin >> a >> b; adj[a].pb(b); adj[b].pb(a);
}
dfs(1,-1);
FOR(k,1,19){
FOR(i,1,N){
if (twok[i][k-1] == -1) twok[i][k] = -1;
else twok[i][k] = twok[twok[i][k-1]][k-1];
}
}
cin >> M;
FOR(i,1,M){
cin >> a >> b >> c;
A[lca(a,b)].pb(pii(pi(a,b), c));
}
fw = new FW(N);
compdp(1,-1);
cout << dp[1];
}
Compilation message (stderr)
election_campaign.cpp: In function 'void compdp(ll, ll)':
election_campaign.cpp:91:17: warning: unused variable 'ap' [-Wunused-variable]
91 | int ap = kpar(a,depth[a]-depth[x]-1);
| ^~
election_campaign.cpp:95:17: warning: unused variable 'bp' [-Wunused-variable]
95 | int bp = kpar(b,depth[b]-depth[x]-1);
| ^~
# | 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... |