#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#define pb push_back
#define F first
#define S second
#define ld long double
#define all(a) a.begin(),a.end()
#define pii pair <int,int>
#define PII pair<pii , pii>
#define sz(v) (int)v.size()
#define rep(i , a , b) for(int i=a;i <= (b);i++)
#define per(i , a , b) for(int i=a;i >= (b);i--)
#define deb(x) cout <<#x << " : " << x << "\n" ;
using namespace std ;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1e6+10 + 10, inf = 1e9+10 , lg = 19 , mod = 1000696969 ;
int fen[maxn][2] , sm[maxn] , dp[maxn] , par[maxn][lg+1] , n ,st[maxn] , en[maxn] , c= 1 , dis[maxn] ;
vector <int> G[maxn] ;
vector <pair<pii,int> > Q[maxn];
int up(int v ,int d){
rep(i , 0 ,lg){
if(d>>i&1) v = par[v][i] ;
}
return v ;
}
int lca(int v ,int u){
if(dis[v] < dis[u])swap(v, u);
v = up(v ,dis[v]-dis[u]);
per(i , lg, 0){
if(par[v][i] != par[u][i]){
v = par[v][i] ;
u = par[u][i] ;
}
}
if(v==u)return v ;
return par[v][0] ;
}
void upd(int x, int w , int t){
while(x <= n){
fen[x][t] += w ;
x += x&-x ;
}
}
int que(int x , int t){
int ans = 0;
while(x){
ans = (fen[x][t] +ans);
x -= x&-x ;
}
return ans ;
}
void bui(int v , int p =0){
par[v][0] = p ;
rep(i ,1 ,lg){
par[v][i] = par[par[v][i-1]][i-1] ;
}
st[v] = c; c++;
for(int u : G[v]){
if(u == p)continue ;
dis[u] = dis[v]+1;
bui(u,v);
}
en[v] = c-1 ;
}
void dfs(int v ,int p =0){
for(int u : G[v]){
if(u == p)continue ;
dfs(u , v);
sm[v] += dp[u] ;
}
dp[v] = sm[v];
for(auto x :Q[v]){
int a= x.F.F , b = x.F.S , w = x.S ;
int ans = que(st[a] , 0) + que(st[b] , 0) - que(st[a] , 1) - que(st[b] ,1) + sm[v] + w;
dp[v] = max(dp[v] , ans) ;
}
upd(st[v] , sm[v] , 0) ;upd(en[v]+1, -sm[v], 0) ;
upd(st[v] , dp[v] , 1);upd(en[v]+1 ,-dp[v] , 1);
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n ;
rep(i , 1, n-1){
int v, u ;
cin >> v >> u ;
G[v].pb(u);
G[u].pb(v);
}
bui(1);
int q;
cin >> q ;
while(q--){
int v, u , w ;
cin >> v >> u >> w ;
Q[lca(v,u)].pb({{v,u},w});
}
dfs(1);
cout << dp[1] << "\n";
return 0 ;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
57944 KB |
Output is correct |
2 |
Correct |
12 ms |
58100 KB |
Output is correct |
3 |
Correct |
14 ms |
57944 KB |
Output is correct |
4 |
Correct |
13 ms |
57948 KB |
Output is correct |
5 |
Correct |
71 ms |
70996 KB |
Output is correct |
6 |
Correct |
48 ms |
80176 KB |
Output is correct |
7 |
Correct |
78 ms |
76964 KB |
Output is correct |
8 |
Correct |
51 ms |
70996 KB |
Output is correct |
9 |
Correct |
65 ms |
75092 KB |
Output is correct |
10 |
Correct |
53 ms |
71092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
58072 KB |
Output is correct |
2 |
Correct |
12 ms |
57948 KB |
Output is correct |
3 |
Correct |
13 ms |
58204 KB |
Output is correct |
4 |
Correct |
90 ms |
83544 KB |
Output is correct |
5 |
Correct |
89 ms |
83528 KB |
Output is correct |
6 |
Correct |
88 ms |
83540 KB |
Output is correct |
7 |
Correct |
99 ms |
83540 KB |
Output is correct |
8 |
Correct |
91 ms |
83608 KB |
Output is correct |
9 |
Correct |
91 ms |
83484 KB |
Output is correct |
10 |
Correct |
93 ms |
83496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
58072 KB |
Output is correct |
2 |
Correct |
12 ms |
57948 KB |
Output is correct |
3 |
Correct |
13 ms |
58204 KB |
Output is correct |
4 |
Correct |
90 ms |
83544 KB |
Output is correct |
5 |
Correct |
89 ms |
83528 KB |
Output is correct |
6 |
Correct |
88 ms |
83540 KB |
Output is correct |
7 |
Correct |
99 ms |
83540 KB |
Output is correct |
8 |
Correct |
91 ms |
83608 KB |
Output is correct |
9 |
Correct |
91 ms |
83484 KB |
Output is correct |
10 |
Correct |
93 ms |
83496 KB |
Output is correct |
11 |
Correct |
21 ms |
58968 KB |
Output is correct |
12 |
Correct |
110 ms |
83852 KB |
Output is correct |
13 |
Correct |
95 ms |
83956 KB |
Output is correct |
14 |
Correct |
90 ms |
83796 KB |
Output is correct |
15 |
Correct |
93 ms |
83796 KB |
Output is correct |
16 |
Correct |
98 ms |
83828 KB |
Output is correct |
17 |
Correct |
93 ms |
83792 KB |
Output is correct |
18 |
Correct |
92 ms |
83796 KB |
Output is correct |
19 |
Correct |
86 ms |
83812 KB |
Output is correct |
20 |
Correct |
91 ms |
83796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
73884 KB |
Output is correct |
2 |
Correct |
93 ms |
83480 KB |
Output is correct |
3 |
Correct |
123 ms |
79952 KB |
Output is correct |
4 |
Correct |
83 ms |
73996 KB |
Output is correct |
5 |
Correct |
143 ms |
79412 KB |
Output is correct |
6 |
Correct |
90 ms |
73912 KB |
Output is correct |
7 |
Correct |
124 ms |
79056 KB |
Output is correct |
8 |
Correct |
107 ms |
74180 KB |
Output is correct |
9 |
Correct |
90 ms |
83752 KB |
Output is correct |
10 |
Correct |
138 ms |
77924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
57944 KB |
Output is correct |
2 |
Correct |
12 ms |
58100 KB |
Output is correct |
3 |
Correct |
14 ms |
57944 KB |
Output is correct |
4 |
Correct |
13 ms |
57948 KB |
Output is correct |
5 |
Correct |
71 ms |
70996 KB |
Output is correct |
6 |
Correct |
48 ms |
80176 KB |
Output is correct |
7 |
Correct |
78 ms |
76964 KB |
Output is correct |
8 |
Correct |
51 ms |
70996 KB |
Output is correct |
9 |
Correct |
65 ms |
75092 KB |
Output is correct |
10 |
Correct |
53 ms |
71092 KB |
Output is correct |
11 |
Correct |
13 ms |
58176 KB |
Output is correct |
12 |
Correct |
13 ms |
58204 KB |
Output is correct |
13 |
Correct |
13 ms |
58204 KB |
Output is correct |
14 |
Correct |
13 ms |
58144 KB |
Output is correct |
15 |
Correct |
13 ms |
57948 KB |
Output is correct |
16 |
Correct |
13 ms |
58204 KB |
Output is correct |
17 |
Correct |
13 ms |
57944 KB |
Output is correct |
18 |
Correct |
13 ms |
58200 KB |
Output is correct |
19 |
Correct |
14 ms |
57948 KB |
Output is correct |
20 |
Correct |
13 ms |
58204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
57944 KB |
Output is correct |
2 |
Correct |
12 ms |
58100 KB |
Output is correct |
3 |
Correct |
14 ms |
57944 KB |
Output is correct |
4 |
Correct |
13 ms |
57948 KB |
Output is correct |
5 |
Correct |
71 ms |
70996 KB |
Output is correct |
6 |
Correct |
48 ms |
80176 KB |
Output is correct |
7 |
Correct |
78 ms |
76964 KB |
Output is correct |
8 |
Correct |
51 ms |
70996 KB |
Output is correct |
9 |
Correct |
65 ms |
75092 KB |
Output is correct |
10 |
Correct |
53 ms |
71092 KB |
Output is correct |
11 |
Correct |
13 ms |
58072 KB |
Output is correct |
12 |
Correct |
12 ms |
57948 KB |
Output is correct |
13 |
Correct |
13 ms |
58204 KB |
Output is correct |
14 |
Correct |
90 ms |
83544 KB |
Output is correct |
15 |
Correct |
89 ms |
83528 KB |
Output is correct |
16 |
Correct |
88 ms |
83540 KB |
Output is correct |
17 |
Correct |
99 ms |
83540 KB |
Output is correct |
18 |
Correct |
91 ms |
83608 KB |
Output is correct |
19 |
Correct |
91 ms |
83484 KB |
Output is correct |
20 |
Correct |
93 ms |
83496 KB |
Output is correct |
21 |
Correct |
21 ms |
58968 KB |
Output is correct |
22 |
Correct |
110 ms |
83852 KB |
Output is correct |
23 |
Correct |
95 ms |
83956 KB |
Output is correct |
24 |
Correct |
90 ms |
83796 KB |
Output is correct |
25 |
Correct |
93 ms |
83796 KB |
Output is correct |
26 |
Correct |
98 ms |
83828 KB |
Output is correct |
27 |
Correct |
93 ms |
83792 KB |
Output is correct |
28 |
Correct |
92 ms |
83796 KB |
Output is correct |
29 |
Correct |
86 ms |
83812 KB |
Output is correct |
30 |
Correct |
91 ms |
83796 KB |
Output is correct |
31 |
Correct |
95 ms |
73884 KB |
Output is correct |
32 |
Correct |
93 ms |
83480 KB |
Output is correct |
33 |
Correct |
123 ms |
79952 KB |
Output is correct |
34 |
Correct |
83 ms |
73996 KB |
Output is correct |
35 |
Correct |
143 ms |
79412 KB |
Output is correct |
36 |
Correct |
90 ms |
73912 KB |
Output is correct |
37 |
Correct |
124 ms |
79056 KB |
Output is correct |
38 |
Correct |
107 ms |
74180 KB |
Output is correct |
39 |
Correct |
90 ms |
83752 KB |
Output is correct |
40 |
Correct |
138 ms |
77924 KB |
Output is correct |
41 |
Correct |
13 ms |
58176 KB |
Output is correct |
42 |
Correct |
13 ms |
58204 KB |
Output is correct |
43 |
Correct |
13 ms |
58204 KB |
Output is correct |
44 |
Correct |
13 ms |
58144 KB |
Output is correct |
45 |
Correct |
13 ms |
57948 KB |
Output is correct |
46 |
Correct |
13 ms |
58204 KB |
Output is correct |
47 |
Correct |
13 ms |
57944 KB |
Output is correct |
48 |
Correct |
13 ms |
58200 KB |
Output is correct |
49 |
Correct |
14 ms |
57948 KB |
Output is correct |
50 |
Correct |
13 ms |
58204 KB |
Output is correct |
51 |
Correct |
110 ms |
74344 KB |
Output is correct |
52 |
Correct |
97 ms |
83800 KB |
Output is correct |
53 |
Correct |
152 ms |
78460 KB |
Output is correct |
54 |
Correct |
81 ms |
74120 KB |
Output is correct |
55 |
Correct |
101 ms |
74240 KB |
Output is correct |
56 |
Correct |
105 ms |
83828 KB |
Output is correct |
57 |
Correct |
162 ms |
79188 KB |
Output is correct |
58 |
Correct |
104 ms |
74052 KB |
Output is correct |
59 |
Correct |
116 ms |
74312 KB |
Output is correct |
60 |
Correct |
100 ms |
83928 KB |
Output is correct |
61 |
Correct |
142 ms |
79236 KB |
Output is correct |
62 |
Correct |
107 ms |
73920 KB |
Output is correct |
63 |
Correct |
119 ms |
74052 KB |
Output is correct |
64 |
Correct |
116 ms |
76220 KB |
Output is correct |
65 |
Correct |
179 ms |
73036 KB |
Output is correct |
66 |
Correct |
112 ms |
68364 KB |
Output is correct |
67 |
Correct |
100 ms |
70152 KB |
Output is correct |
68 |
Correct |
94 ms |
78020 KB |
Output is correct |
69 |
Correct |
124 ms |
73040 KB |
Output is correct |
70 |
Correct |
82 ms |
74336 KB |
Output is correct |