#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 = 1e5;
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 |
3 ms |
5248 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5248 KB |
Output is correct |
5 |
Correct |
113 ms |
19704 KB |
Output is correct |
6 |
Correct |
64 ms |
27384 KB |
Output is correct |
7 |
Correct |
107 ms |
24684 KB |
Output is correct |
8 |
Correct |
77 ms |
20088 KB |
Output is correct |
9 |
Correct |
126 ms |
23164 KB |
Output is correct |
10 |
Correct |
99 ms |
20088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
156 ms |
30840 KB |
Output is correct |
5 |
Correct |
134 ms |
30968 KB |
Output is correct |
6 |
Correct |
134 ms |
30972 KB |
Output is correct |
7 |
Correct |
146 ms |
30840 KB |
Output is correct |
8 |
Correct |
133 ms |
30840 KB |
Output is correct |
9 |
Correct |
122 ms |
30968 KB |
Output is correct |
10 |
Correct |
134 ms |
30968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
156 ms |
30840 KB |
Output is correct |
5 |
Correct |
134 ms |
30968 KB |
Output is correct |
6 |
Correct |
134 ms |
30972 KB |
Output is correct |
7 |
Correct |
146 ms |
30840 KB |
Output is correct |
8 |
Correct |
133 ms |
30840 KB |
Output is correct |
9 |
Correct |
122 ms |
30968 KB |
Output is correct |
10 |
Correct |
134 ms |
30968 KB |
Output is correct |
11 |
Correct |
14 ms |
6144 KB |
Output is correct |
12 |
Correct |
151 ms |
31224 KB |
Output is correct |
13 |
Correct |
157 ms |
31252 KB |
Output is correct |
14 |
Correct |
126 ms |
31220 KB |
Output is correct |
15 |
Correct |
139 ms |
31224 KB |
Output is correct |
16 |
Correct |
136 ms |
31224 KB |
Output is correct |
17 |
Correct |
144 ms |
31216 KB |
Output is correct |
18 |
Correct |
159 ms |
31224 KB |
Output is correct |
19 |
Correct |
136 ms |
31224 KB |
Output is correct |
20 |
Correct |
138 ms |
31224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
212 ms |
22640 KB |
Output is correct |
2 |
Correct |
140 ms |
30968 KB |
Output is correct |
3 |
Correct |
276 ms |
27768 KB |
Output is correct |
4 |
Correct |
151 ms |
22900 KB |
Output is correct |
5 |
Correct |
232 ms |
27512 KB |
Output is correct |
6 |
Correct |
132 ms |
23288 KB |
Output is correct |
7 |
Correct |
232 ms |
27128 KB |
Output is correct |
8 |
Incorrect |
203 ms |
23160 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5248 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5248 KB |
Output is correct |
5 |
Correct |
113 ms |
19704 KB |
Output is correct |
6 |
Correct |
64 ms |
27384 KB |
Output is correct |
7 |
Correct |
107 ms |
24684 KB |
Output is correct |
8 |
Correct |
77 ms |
20088 KB |
Output is correct |
9 |
Correct |
126 ms |
23164 KB |
Output is correct |
10 |
Correct |
99 ms |
20088 KB |
Output is correct |
11 |
Correct |
5 ms |
5248 KB |
Output is correct |
12 |
Correct |
5 ms |
5376 KB |
Output is correct |
13 |
Correct |
5 ms |
5376 KB |
Output is correct |
14 |
Correct |
5 ms |
5376 KB |
Output is correct |
15 |
Correct |
4 ms |
5248 KB |
Output is correct |
16 |
Correct |
4 ms |
5376 KB |
Output is correct |
17 |
Correct |
4 ms |
5248 KB |
Output is correct |
18 |
Correct |
4 ms |
5376 KB |
Output is correct |
19 |
Correct |
4 ms |
5248 KB |
Output is correct |
20 |
Correct |
4 ms |
5376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5248 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5248 KB |
Output is correct |
5 |
Correct |
113 ms |
19704 KB |
Output is correct |
6 |
Correct |
64 ms |
27384 KB |
Output is correct |
7 |
Correct |
107 ms |
24684 KB |
Output is correct |
8 |
Correct |
77 ms |
20088 KB |
Output is correct |
9 |
Correct |
126 ms |
23164 KB |
Output is correct |
10 |
Correct |
99 ms |
20088 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 |
156 ms |
30840 KB |
Output is correct |
15 |
Correct |
134 ms |
30968 KB |
Output is correct |
16 |
Correct |
134 ms |
30972 KB |
Output is correct |
17 |
Correct |
146 ms |
30840 KB |
Output is correct |
18 |
Correct |
133 ms |
30840 KB |
Output is correct |
19 |
Correct |
122 ms |
30968 KB |
Output is correct |
20 |
Correct |
134 ms |
30968 KB |
Output is correct |
21 |
Correct |
14 ms |
6144 KB |
Output is correct |
22 |
Correct |
151 ms |
31224 KB |
Output is correct |
23 |
Correct |
157 ms |
31252 KB |
Output is correct |
24 |
Correct |
126 ms |
31220 KB |
Output is correct |
25 |
Correct |
139 ms |
31224 KB |
Output is correct |
26 |
Correct |
136 ms |
31224 KB |
Output is correct |
27 |
Correct |
144 ms |
31216 KB |
Output is correct |
28 |
Correct |
159 ms |
31224 KB |
Output is correct |
29 |
Correct |
136 ms |
31224 KB |
Output is correct |
30 |
Correct |
138 ms |
31224 KB |
Output is correct |
31 |
Correct |
212 ms |
22640 KB |
Output is correct |
32 |
Correct |
140 ms |
30968 KB |
Output is correct |
33 |
Correct |
276 ms |
27768 KB |
Output is correct |
34 |
Correct |
151 ms |
22900 KB |
Output is correct |
35 |
Correct |
232 ms |
27512 KB |
Output is correct |
36 |
Correct |
132 ms |
23288 KB |
Output is correct |
37 |
Correct |
232 ms |
27128 KB |
Output is correct |
38 |
Incorrect |
203 ms |
23160 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |