// FUCKED UP FUCKED UP FUCKED UP FUCKED UP FUCKED UP
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2")
#define F first
#define S second
#define pb push_back
#define SZ(x) (ll)(x.size())
#define all(x) x.begin(),x.end()
#define MP make_pair
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll maxn=1e5+10, maxm=1e6+10, lg=17, mod=1e9+7, inf=1e18;
#define git(x) (x&-x)
ll n,m,A[maxn],B[maxn],C[maxn],par[maxn][lg+1],ss[maxn][lg+1],st[maxn],ft[maxn],tt,h[maxn],bit[maxn],dp[maxn];
vector<ll> g[maxn],Q[maxn];
void add(ll i,ll x){for(;i<maxn;i+=git(i))bit[i]+=x;}
ll get(ll i){ll ret=0;for(;i;i-=git(i))ret+=bit[i];return ret;}
void pfs(ll v=1,ll p=0){
st[v]=++tt;
h[v]=h[par[v][0]=p]+1; for(int i=1;i<=lg;i++) par[v][i]=par[par[v][i-1]][i-1];
for(auto u:g[v])if(u!=p) pfs(u,v);
ft[v]=tt+1;
}
ll parx(ll v,ll x){
if(x<=0) return v;
for(int i=0;i<=lg;i++)if(x>>i&1) v=par[v][i];
return v;
}
ll lca(ll v,ll u){
if(h[v]<h[u]) swap(v,u);
v=parx(v,h[v]-h[u]);
if(v==u) return v;
for(int i=lg;i>=0;i--)if(par[v][i]!=par[u][i]) v=par[v][i], u=par[u][i];
return par[v][0];
}
void dfs(ll v=1,ll p=0){
ll bac=0;
for(auto u:g[v])if(u!=p) dfs(u,v), bac+=dp[u];
dp[v]=bac;
for(auto i:Q[v]) dp[v]=max(dp[v],C[i]+bac+get(st[A[i]])+get(st[B[i]]));
//cout<<v<<' '<<dp[v]<<endl;
add(st[v],bac-dp[v]);
add(ft[v],-(bac-dp[v]));
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<n;i++){
ll v,u; cin>>v>>u;
g[v].pb(u), g[u].pb(v);
}
pfs();
cin>>m;
for(int i=0;i<m;i++) cin>>A[i]>>B[i]>>C[i], Q[lca(A[i],B[i])].pb(i);
dfs();
cout<<dp[1];
return 0;
}
// a denial a denial a denial a denial a denial a denial
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
5 ms |
5376 KB |
Output is correct |
5 |
Correct |
141 ms |
28152 KB |
Output is correct |
6 |
Correct |
77 ms |
35192 KB |
Output is correct |
7 |
Correct |
144 ms |
32828 KB |
Output is correct |
8 |
Correct |
91 ms |
28280 KB |
Output is correct |
9 |
Correct |
135 ms |
31352 KB |
Output is correct |
10 |
Correct |
84 ms |
28280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5376 KB |
Output is correct |
4 |
Correct |
167 ms |
40440 KB |
Output is correct |
5 |
Correct |
184 ms |
40568 KB |
Output is correct |
6 |
Correct |
149 ms |
40440 KB |
Output is correct |
7 |
Correct |
167 ms |
40316 KB |
Output is correct |
8 |
Correct |
202 ms |
40440 KB |
Output is correct |
9 |
Correct |
141 ms |
40440 KB |
Output is correct |
10 |
Correct |
162 ms |
40440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5376 KB |
Output is correct |
4 |
Correct |
167 ms |
40440 KB |
Output is correct |
5 |
Correct |
184 ms |
40568 KB |
Output is correct |
6 |
Correct |
149 ms |
40440 KB |
Output is correct |
7 |
Correct |
167 ms |
40316 KB |
Output is correct |
8 |
Correct |
202 ms |
40440 KB |
Output is correct |
9 |
Correct |
141 ms |
40440 KB |
Output is correct |
10 |
Correct |
162 ms |
40440 KB |
Output is correct |
11 |
Correct |
14 ms |
6528 KB |
Output is correct |
12 |
Correct |
199 ms |
40696 KB |
Output is correct |
13 |
Correct |
207 ms |
40824 KB |
Output is correct |
14 |
Correct |
173 ms |
40696 KB |
Output is correct |
15 |
Correct |
213 ms |
40696 KB |
Output is correct |
16 |
Correct |
171 ms |
40736 KB |
Output is correct |
17 |
Correct |
186 ms |
40700 KB |
Output is correct |
18 |
Correct |
184 ms |
40824 KB |
Output is correct |
19 |
Correct |
167 ms |
40676 KB |
Output is correct |
20 |
Correct |
171 ms |
40696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
239 ms |
32876 KB |
Output is correct |
2 |
Correct |
193 ms |
40440 KB |
Output is correct |
3 |
Correct |
434 ms |
37752 KB |
Output is correct |
4 |
Correct |
207 ms |
33008 KB |
Output is correct |
5 |
Correct |
355 ms |
37240 KB |
Output is correct |
6 |
Correct |
164 ms |
32964 KB |
Output is correct |
7 |
Correct |
405 ms |
36980 KB |
Output is correct |
8 |
Correct |
214 ms |
33016 KB |
Output is correct |
9 |
Correct |
168 ms |
40568 KB |
Output is correct |
10 |
Correct |
283 ms |
36008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
5 ms |
5376 KB |
Output is correct |
5 |
Correct |
141 ms |
28152 KB |
Output is correct |
6 |
Correct |
77 ms |
35192 KB |
Output is correct |
7 |
Correct |
144 ms |
32828 KB |
Output is correct |
8 |
Correct |
91 ms |
28280 KB |
Output is correct |
9 |
Correct |
135 ms |
31352 KB |
Output is correct |
10 |
Correct |
84 ms |
28280 KB |
Output is correct |
11 |
Correct |
5 ms |
5408 KB |
Output is correct |
12 |
Correct |
5 ms |
5504 KB |
Output is correct |
13 |
Correct |
5 ms |
5376 KB |
Output is correct |
14 |
Correct |
6 ms |
5408 KB |
Output is correct |
15 |
Correct |
6 ms |
5376 KB |
Output is correct |
16 |
Correct |
6 ms |
5376 KB |
Output is correct |
17 |
Correct |
5 ms |
5376 KB |
Output is correct |
18 |
Correct |
6 ms |
5376 KB |
Output is correct |
19 |
Correct |
6 ms |
5376 KB |
Output is correct |
20 |
Correct |
5 ms |
5504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
5 ms |
5376 KB |
Output is correct |
5 |
Correct |
141 ms |
28152 KB |
Output is correct |
6 |
Correct |
77 ms |
35192 KB |
Output is correct |
7 |
Correct |
144 ms |
32828 KB |
Output is correct |
8 |
Correct |
91 ms |
28280 KB |
Output is correct |
9 |
Correct |
135 ms |
31352 KB |
Output is correct |
10 |
Correct |
84 ms |
28280 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5376 KB |
Output is correct |
14 |
Correct |
167 ms |
40440 KB |
Output is correct |
15 |
Correct |
184 ms |
40568 KB |
Output is correct |
16 |
Correct |
149 ms |
40440 KB |
Output is correct |
17 |
Correct |
167 ms |
40316 KB |
Output is correct |
18 |
Correct |
202 ms |
40440 KB |
Output is correct |
19 |
Correct |
141 ms |
40440 KB |
Output is correct |
20 |
Correct |
162 ms |
40440 KB |
Output is correct |
21 |
Correct |
14 ms |
6528 KB |
Output is correct |
22 |
Correct |
199 ms |
40696 KB |
Output is correct |
23 |
Correct |
207 ms |
40824 KB |
Output is correct |
24 |
Correct |
173 ms |
40696 KB |
Output is correct |
25 |
Correct |
213 ms |
40696 KB |
Output is correct |
26 |
Correct |
171 ms |
40736 KB |
Output is correct |
27 |
Correct |
186 ms |
40700 KB |
Output is correct |
28 |
Correct |
184 ms |
40824 KB |
Output is correct |
29 |
Correct |
167 ms |
40676 KB |
Output is correct |
30 |
Correct |
171 ms |
40696 KB |
Output is correct |
31 |
Correct |
239 ms |
32876 KB |
Output is correct |
32 |
Correct |
193 ms |
40440 KB |
Output is correct |
33 |
Correct |
434 ms |
37752 KB |
Output is correct |
34 |
Correct |
207 ms |
33008 KB |
Output is correct |
35 |
Correct |
355 ms |
37240 KB |
Output is correct |
36 |
Correct |
164 ms |
32964 KB |
Output is correct |
37 |
Correct |
405 ms |
36980 KB |
Output is correct |
38 |
Correct |
214 ms |
33016 KB |
Output is correct |
39 |
Correct |
168 ms |
40568 KB |
Output is correct |
40 |
Correct |
283 ms |
36008 KB |
Output is correct |
41 |
Correct |
5 ms |
5408 KB |
Output is correct |
42 |
Correct |
5 ms |
5504 KB |
Output is correct |
43 |
Correct |
5 ms |
5376 KB |
Output is correct |
44 |
Correct |
6 ms |
5408 KB |
Output is correct |
45 |
Correct |
6 ms |
5376 KB |
Output is correct |
46 |
Correct |
6 ms |
5376 KB |
Output is correct |
47 |
Correct |
5 ms |
5376 KB |
Output is correct |
48 |
Correct |
6 ms |
5376 KB |
Output is correct |
49 |
Correct |
6 ms |
5376 KB |
Output is correct |
50 |
Correct |
5 ms |
5504 KB |
Output is correct |
51 |
Correct |
222 ms |
33272 KB |
Output is correct |
52 |
Correct |
175 ms |
40696 KB |
Output is correct |
53 |
Correct |
284 ms |
36332 KB |
Output is correct |
54 |
Correct |
137 ms |
33140 KB |
Output is correct |
55 |
Correct |
201 ms |
33100 KB |
Output is correct |
56 |
Correct |
206 ms |
40696 KB |
Output is correct |
57 |
Correct |
242 ms |
37112 KB |
Output is correct |
58 |
Correct |
147 ms |
33140 KB |
Output is correct |
59 |
Correct |
202 ms |
33272 KB |
Output is correct |
60 |
Correct |
156 ms |
40696 KB |
Output is correct |
61 |
Correct |
326 ms |
37240 KB |
Output is correct |
62 |
Correct |
145 ms |
33016 KB |
Output is correct |
63 |
Correct |
220 ms |
32960 KB |
Output is correct |
64 |
Correct |
207 ms |
40696 KB |
Output is correct |
65 |
Correct |
358 ms |
37104 KB |
Output is correct |
66 |
Correct |
167 ms |
33268 KB |
Output is correct |
67 |
Correct |
245 ms |
33004 KB |
Output is correct |
68 |
Correct |
180 ms |
40696 KB |
Output is correct |
69 |
Correct |
253 ms |
35960 KB |
Output is correct |
70 |
Correct |
144 ms |
33408 KB |
Output is correct |