#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
#include <time.h>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int i_inf = 1e9;
const ll inf = 1e17;
const ll mod = 1000000007;
const ld eps = 1e-13;
const ld pi = 3.14159265359;
mt19937 _rand(time(NULL));
clock_t timer = clock();
const int mxn = 2e5;
int n, m;
vector<int> g[mxn];
int depth[mxn];
int sparse[mxn][20];
int sz[mxn];
int pos[mxn];
int temp;
void dfs(int u, int p){
pos[u] = ++temp;
sparse[u][0] = p;
fr(i, 1, 20) sparse[u][i] = sparse[sparse[u][i-1]][i-1];
sz[u] = 1;
for(auto e : g[u]){
if(e == p) continue;
depth[e] = depth[u]+1;
dfs(e, u);
sz[u] += sz[e];
}
}
int lca(int a, int b){
int d = min(depth[a], depth[b]);
for(int i = 19; i >= 0; i --){
if(depth[a]-(1<<i) >= d) a = sparse[a][i];
if(depth[b]-(1<<i) >= d) b = sparse[b][i];
}
if(a == b) return a;
for(int i = 19; i >= 0; i --){
if(sparse[a][i] != sparse[b][i]){
a = sparse[a][i];
b = sparse[b][i];
}
}
return sparse[a][0];
}
int a[mxn], b[mxn], c[mxn];
vector<int> v[mxn];
int bit[2][mxn];
void update(int t, int k, int val){
while(k < mxn){
bit[t][k] += val;
k += k&-k;
}
}
int sum(int t, int k){
int s = 0;
while(k > 0){
s += bit[t][k];
k -= k&-k;
}
return s;
}
int dp[mxn];
void dfs2(int u, int p){
int _sum = 0;
for(auto e : g[u]){
if(e == p) continue;
dfs2(e, u);
_sum += dp[e];
}
dp[u] = _sum;
update(0, pos[u], _sum);
update(0, pos[u]+sz[u], -_sum);
for(auto e : v[u]){
int tot = sum(0, pos[a[e]]) + sum(0, pos[b[e]]) - sum(0, pos[u]) - sum(1, pos[a[e]]) - sum(1, pos[b[e]]);
tot += c[e];
dp[u] = max(dp[u], tot);
}
update(1, pos[u], dp[u]);
update(1, pos[u]+sz[u], -dp[u]);
}
void solve(){
cin >> n;
fr(i, 0, n-1){
int u, v;
cin >> u >> v;
--u, --v;
g[u].pb(v);
g[v].pb(u);
}
dfs(0, 0);
cin >> m;
fr(i, 0, m){
cin >> a[i] >> b[i] >> c[i];
--a[i], --b[i];
int l = lca(a[i], b[i]);
v[l].pb(i);
}
dfs2(0, 0);
cout<<dp[0]<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9856 KB |
Output is correct |
2 |
Correct |
7 ms |
9856 KB |
Output is correct |
3 |
Correct |
6 ms |
9856 KB |
Output is correct |
4 |
Correct |
6 ms |
9984 KB |
Output is correct |
5 |
Correct |
124 ms |
23332 KB |
Output is correct |
6 |
Correct |
74 ms |
30956 KB |
Output is correct |
7 |
Correct |
155 ms |
28204 KB |
Output is correct |
8 |
Correct |
91 ms |
23672 KB |
Output is correct |
9 |
Correct |
120 ms |
26744 KB |
Output is correct |
10 |
Correct |
88 ms |
23672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9856 KB |
Output is correct |
2 |
Correct |
6 ms |
9856 KB |
Output is correct |
3 |
Correct |
6 ms |
10112 KB |
Output is correct |
4 |
Correct |
153 ms |
33272 KB |
Output is correct |
5 |
Correct |
145 ms |
33272 KB |
Output is correct |
6 |
Correct |
133 ms |
33272 KB |
Output is correct |
7 |
Correct |
139 ms |
33272 KB |
Output is correct |
8 |
Correct |
162 ms |
33272 KB |
Output is correct |
9 |
Correct |
137 ms |
33272 KB |
Output is correct |
10 |
Correct |
176 ms |
33272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9856 KB |
Output is correct |
2 |
Correct |
6 ms |
9856 KB |
Output is correct |
3 |
Correct |
6 ms |
10112 KB |
Output is correct |
4 |
Correct |
153 ms |
33272 KB |
Output is correct |
5 |
Correct |
145 ms |
33272 KB |
Output is correct |
6 |
Correct |
133 ms |
33272 KB |
Output is correct |
7 |
Correct |
139 ms |
33272 KB |
Output is correct |
8 |
Correct |
162 ms |
33272 KB |
Output is correct |
9 |
Correct |
137 ms |
33272 KB |
Output is correct |
10 |
Correct |
176 ms |
33272 KB |
Output is correct |
11 |
Correct |
17 ms |
10496 KB |
Output is correct |
12 |
Correct |
148 ms |
33144 KB |
Output is correct |
13 |
Correct |
155 ms |
33272 KB |
Output is correct |
14 |
Correct |
149 ms |
33272 KB |
Output is correct |
15 |
Correct |
148 ms |
33264 KB |
Output is correct |
16 |
Correct |
142 ms |
33272 KB |
Output is correct |
17 |
Correct |
155 ms |
33144 KB |
Output is correct |
18 |
Correct |
145 ms |
33400 KB |
Output is correct |
19 |
Correct |
129 ms |
33272 KB |
Output is correct |
20 |
Correct |
167 ms |
33180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
183 ms |
24948 KB |
Output is correct |
2 |
Correct |
147 ms |
33272 KB |
Output is correct |
3 |
Correct |
229 ms |
30200 KB |
Output is correct |
4 |
Correct |
130 ms |
25204 KB |
Output is correct |
5 |
Correct |
253 ms |
29816 KB |
Output is correct |
6 |
Correct |
156 ms |
25464 KB |
Output is correct |
7 |
Correct |
277 ms |
29568 KB |
Output is correct |
8 |
Correct |
188 ms |
25336 KB |
Output is correct |
9 |
Correct |
145 ms |
35704 KB |
Output is correct |
10 |
Correct |
281 ms |
30964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9856 KB |
Output is correct |
2 |
Correct |
7 ms |
9856 KB |
Output is correct |
3 |
Correct |
6 ms |
9856 KB |
Output is correct |
4 |
Correct |
6 ms |
9984 KB |
Output is correct |
5 |
Correct |
124 ms |
23332 KB |
Output is correct |
6 |
Correct |
74 ms |
30956 KB |
Output is correct |
7 |
Correct |
155 ms |
28204 KB |
Output is correct |
8 |
Correct |
91 ms |
23672 KB |
Output is correct |
9 |
Correct |
120 ms |
26744 KB |
Output is correct |
10 |
Correct |
88 ms |
23672 KB |
Output is correct |
11 |
Correct |
7 ms |
9984 KB |
Output is correct |
12 |
Correct |
7 ms |
10112 KB |
Output is correct |
13 |
Correct |
10 ms |
10112 KB |
Output is correct |
14 |
Correct |
7 ms |
9984 KB |
Output is correct |
15 |
Correct |
7 ms |
9984 KB |
Output is correct |
16 |
Correct |
7 ms |
9984 KB |
Output is correct |
17 |
Correct |
7 ms |
9984 KB |
Output is correct |
18 |
Correct |
7 ms |
9984 KB |
Output is correct |
19 |
Correct |
7 ms |
9984 KB |
Output is correct |
20 |
Correct |
7 ms |
10112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9856 KB |
Output is correct |
2 |
Correct |
7 ms |
9856 KB |
Output is correct |
3 |
Correct |
6 ms |
9856 KB |
Output is correct |
4 |
Correct |
6 ms |
9984 KB |
Output is correct |
5 |
Correct |
124 ms |
23332 KB |
Output is correct |
6 |
Correct |
74 ms |
30956 KB |
Output is correct |
7 |
Correct |
155 ms |
28204 KB |
Output is correct |
8 |
Correct |
91 ms |
23672 KB |
Output is correct |
9 |
Correct |
120 ms |
26744 KB |
Output is correct |
10 |
Correct |
88 ms |
23672 KB |
Output is correct |
11 |
Correct |
5 ms |
9856 KB |
Output is correct |
12 |
Correct |
6 ms |
9856 KB |
Output is correct |
13 |
Correct |
6 ms |
10112 KB |
Output is correct |
14 |
Correct |
153 ms |
33272 KB |
Output is correct |
15 |
Correct |
145 ms |
33272 KB |
Output is correct |
16 |
Correct |
133 ms |
33272 KB |
Output is correct |
17 |
Correct |
139 ms |
33272 KB |
Output is correct |
18 |
Correct |
162 ms |
33272 KB |
Output is correct |
19 |
Correct |
137 ms |
33272 KB |
Output is correct |
20 |
Correct |
176 ms |
33272 KB |
Output is correct |
21 |
Correct |
17 ms |
10496 KB |
Output is correct |
22 |
Correct |
148 ms |
33144 KB |
Output is correct |
23 |
Correct |
155 ms |
33272 KB |
Output is correct |
24 |
Correct |
149 ms |
33272 KB |
Output is correct |
25 |
Correct |
148 ms |
33264 KB |
Output is correct |
26 |
Correct |
142 ms |
33272 KB |
Output is correct |
27 |
Correct |
155 ms |
33144 KB |
Output is correct |
28 |
Correct |
145 ms |
33400 KB |
Output is correct |
29 |
Correct |
129 ms |
33272 KB |
Output is correct |
30 |
Correct |
167 ms |
33180 KB |
Output is correct |
31 |
Correct |
183 ms |
24948 KB |
Output is correct |
32 |
Correct |
147 ms |
33272 KB |
Output is correct |
33 |
Correct |
229 ms |
30200 KB |
Output is correct |
34 |
Correct |
130 ms |
25204 KB |
Output is correct |
35 |
Correct |
253 ms |
29816 KB |
Output is correct |
36 |
Correct |
156 ms |
25464 KB |
Output is correct |
37 |
Correct |
277 ms |
29568 KB |
Output is correct |
38 |
Correct |
188 ms |
25336 KB |
Output is correct |
39 |
Correct |
145 ms |
35704 KB |
Output is correct |
40 |
Correct |
281 ms |
30964 KB |
Output is correct |
41 |
Correct |
7 ms |
9984 KB |
Output is correct |
42 |
Correct |
7 ms |
10112 KB |
Output is correct |
43 |
Correct |
10 ms |
10112 KB |
Output is correct |
44 |
Correct |
7 ms |
9984 KB |
Output is correct |
45 |
Correct |
7 ms |
9984 KB |
Output is correct |
46 |
Correct |
7 ms |
9984 KB |
Output is correct |
47 |
Correct |
7 ms |
9984 KB |
Output is correct |
48 |
Correct |
7 ms |
9984 KB |
Output is correct |
49 |
Correct |
7 ms |
9984 KB |
Output is correct |
50 |
Correct |
7 ms |
10112 KB |
Output is correct |
51 |
Correct |
197 ms |
28280 KB |
Output is correct |
52 |
Correct |
144 ms |
35964 KB |
Output is correct |
53 |
Correct |
244 ms |
31476 KB |
Output is correct |
54 |
Correct |
144 ms |
28152 KB |
Output is correct |
55 |
Correct |
176 ms |
27764 KB |
Output is correct |
56 |
Correct |
145 ms |
36088 KB |
Output is correct |
57 |
Correct |
209 ms |
32120 KB |
Output is correct |
58 |
Correct |
136 ms |
28152 KB |
Output is correct |
59 |
Correct |
192 ms |
28152 KB |
Output is correct |
60 |
Correct |
143 ms |
35960 KB |
Output is correct |
61 |
Correct |
228 ms |
32248 KB |
Output is correct |
62 |
Correct |
135 ms |
28080 KB |
Output is correct |
63 |
Correct |
182 ms |
27904 KB |
Output is correct |
64 |
Correct |
151 ms |
35960 KB |
Output is correct |
65 |
Correct |
244 ms |
31988 KB |
Output is correct |
66 |
Correct |
144 ms |
28280 KB |
Output is correct |
67 |
Correct |
181 ms |
27896 KB |
Output is correct |
68 |
Correct |
151 ms |
35960 KB |
Output is correct |
69 |
Correct |
211 ms |
30968 KB |
Output is correct |
70 |
Correct |
160 ms |
28152 KB |
Output is correct |