#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define arr3 array<int, 3>
struct FT{
vector<ll> bit;
int n;
FT(int ns){
n = ns;
bit.resize(n + 1);
}
void add(int v, ll x){
while (v <= n){
bit[v] += x;
v |= (v + 1);
}
}
void add(int l, int r, ll x){
add(l, x);
add(r + 1, -x);
}
ll get(int v){
ll out = 0;
while (v > 0){
out += bit[v];
v = (v & (v + 1)) - 1;
}
return out;
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
vector<int> g[n + 1];
for (int i = 1; i < n; i++){
int x, y; cin>>x>>y;
g[x].pb(y);
g[y].pb(x);
}
const int lg = log2(n);
vector<vector<int>> pw(n + 1, vector<int>(lg + 1));
vector<int> tin(n + 1), tout(n + 1);
int timer = 0;
function<void(int, int)> fill = [&](int v, int pr){
pw[v][0] = pr;
for (int i = 1; i <= lg; i++){
pw[v][i] = pw[pw[v][i - 1]][i - 1];
}
tin[v] = ++timer;
for (int i: g[v]){
if (i == pr) continue;
fill(i, v);
}
tout[v] = timer;
};
fill(1, 1);
auto check = [&](int x, int y){
return (tin[x] <= tin[y] && tout[x] >= tout[y]);
};
auto lca = [&](int x, int y){
if (check(x, y)) return x;
if (check(y, x)) return y;
for (int i = lg; i >= 0; i--){
if (!check(pw[x][i], y)){
x = pw[x][i];
}
}
return pw[x][0];
};
vector<arr3> t[n + 1];
int m; cin>>m;
while (m--){
int x, y, c; cin>>x>>y>>c;
t[lca(x, y)].pb({x, y, c});
}
FT T(n);
vector<ll> dp(n + 1);
function<void(int, int)> dfs = [&](int v, int pr){
ll S = 0;
for (int i: g[v]){
if (i == pr) continue;
dfs(i, v);
S += dp[i];
}
dp[v] = S;
for (auto [x, y, c]: t[v]){
dp[v] = max(dp[v], c + S + T.get(tin[x]) + T.get(tin[y]));
}
T.add(tin[v], tout[v], S - dp[v]);
};
dfs(1, 0);
cout<<dp[1]<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
63 ms |
22088 KB |
Output is correct |
6 |
Correct |
52 ms |
32832 KB |
Output is correct |
7 |
Correct |
86 ms |
29000 KB |
Output is correct |
8 |
Correct |
51 ms |
22344 KB |
Output is correct |
9 |
Correct |
74 ms |
26768 KB |
Output is correct |
10 |
Correct |
58 ms |
22400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
75 ms |
36088 KB |
Output is correct |
5 |
Correct |
79 ms |
36152 KB |
Output is correct |
6 |
Correct |
78 ms |
36168 KB |
Output is correct |
7 |
Correct |
79 ms |
36168 KB |
Output is correct |
8 |
Correct |
78 ms |
36168 KB |
Output is correct |
9 |
Correct |
78 ms |
36168 KB |
Output is correct |
10 |
Correct |
80 ms |
36168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
75 ms |
36088 KB |
Output is correct |
5 |
Correct |
79 ms |
36152 KB |
Output is correct |
6 |
Correct |
78 ms |
36168 KB |
Output is correct |
7 |
Correct |
79 ms |
36168 KB |
Output is correct |
8 |
Correct |
78 ms |
36168 KB |
Output is correct |
9 |
Correct |
78 ms |
36168 KB |
Output is correct |
10 |
Correct |
80 ms |
36168 KB |
Output is correct |
11 |
Correct |
6 ms |
1360 KB |
Output is correct |
12 |
Correct |
85 ms |
36504 KB |
Output is correct |
13 |
Correct |
93 ms |
36360 KB |
Output is correct |
14 |
Correct |
76 ms |
36308 KB |
Output is correct |
15 |
Correct |
80 ms |
36356 KB |
Output is correct |
16 |
Correct |
78 ms |
36348 KB |
Output is correct |
17 |
Correct |
82 ms |
36424 KB |
Output is correct |
18 |
Correct |
80 ms |
36436 KB |
Output is correct |
19 |
Correct |
79 ms |
36432 KB |
Output is correct |
20 |
Correct |
73 ms |
36424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
24832 KB |
Output is correct |
2 |
Correct |
70 ms |
36116 KB |
Output is correct |
3 |
Correct |
119 ms |
31816 KB |
Output is correct |
4 |
Correct |
75 ms |
25356 KB |
Output is correct |
5 |
Correct |
106 ms |
31240 KB |
Output is correct |
6 |
Correct |
77 ms |
25112 KB |
Output is correct |
7 |
Correct |
122 ms |
30780 KB |
Output is correct |
8 |
Correct |
105 ms |
24988 KB |
Output is correct |
9 |
Correct |
81 ms |
36168 KB |
Output is correct |
10 |
Correct |
137 ms |
29696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
63 ms |
22088 KB |
Output is correct |
6 |
Correct |
52 ms |
32832 KB |
Output is correct |
7 |
Correct |
86 ms |
29000 KB |
Output is correct |
8 |
Correct |
51 ms |
22344 KB |
Output is correct |
9 |
Correct |
74 ms |
26768 KB |
Output is correct |
10 |
Correct |
58 ms |
22400 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
592 KB |
Output is correct |
13 |
Correct |
1 ms |
688 KB |
Output is correct |
14 |
Correct |
2 ms |
760 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
592 KB |
Output is correct |
17 |
Correct |
1 ms |
592 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
63 ms |
22088 KB |
Output is correct |
6 |
Correct |
52 ms |
32832 KB |
Output is correct |
7 |
Correct |
86 ms |
29000 KB |
Output is correct |
8 |
Correct |
51 ms |
22344 KB |
Output is correct |
9 |
Correct |
74 ms |
26768 KB |
Output is correct |
10 |
Correct |
58 ms |
22400 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
75 ms |
36088 KB |
Output is correct |
15 |
Correct |
79 ms |
36152 KB |
Output is correct |
16 |
Correct |
78 ms |
36168 KB |
Output is correct |
17 |
Correct |
79 ms |
36168 KB |
Output is correct |
18 |
Correct |
78 ms |
36168 KB |
Output is correct |
19 |
Correct |
78 ms |
36168 KB |
Output is correct |
20 |
Correct |
80 ms |
36168 KB |
Output is correct |
21 |
Correct |
6 ms |
1360 KB |
Output is correct |
22 |
Correct |
85 ms |
36504 KB |
Output is correct |
23 |
Correct |
93 ms |
36360 KB |
Output is correct |
24 |
Correct |
76 ms |
36308 KB |
Output is correct |
25 |
Correct |
80 ms |
36356 KB |
Output is correct |
26 |
Correct |
78 ms |
36348 KB |
Output is correct |
27 |
Correct |
82 ms |
36424 KB |
Output is correct |
28 |
Correct |
80 ms |
36436 KB |
Output is correct |
29 |
Correct |
79 ms |
36432 KB |
Output is correct |
30 |
Correct |
73 ms |
36424 KB |
Output is correct |
31 |
Correct |
89 ms |
24832 KB |
Output is correct |
32 |
Correct |
70 ms |
36116 KB |
Output is correct |
33 |
Correct |
119 ms |
31816 KB |
Output is correct |
34 |
Correct |
75 ms |
25356 KB |
Output is correct |
35 |
Correct |
106 ms |
31240 KB |
Output is correct |
36 |
Correct |
77 ms |
25112 KB |
Output is correct |
37 |
Correct |
122 ms |
30780 KB |
Output is correct |
38 |
Correct |
105 ms |
24988 KB |
Output is correct |
39 |
Correct |
81 ms |
36168 KB |
Output is correct |
40 |
Correct |
137 ms |
29696 KB |
Output is correct |
41 |
Correct |
1 ms |
592 KB |
Output is correct |
42 |
Correct |
2 ms |
592 KB |
Output is correct |
43 |
Correct |
1 ms |
688 KB |
Output is correct |
44 |
Correct |
2 ms |
760 KB |
Output is correct |
45 |
Correct |
1 ms |
592 KB |
Output is correct |
46 |
Correct |
1 ms |
592 KB |
Output is correct |
47 |
Correct |
1 ms |
592 KB |
Output is correct |
48 |
Correct |
1 ms |
592 KB |
Output is correct |
49 |
Correct |
1 ms |
592 KB |
Output is correct |
50 |
Correct |
1 ms |
592 KB |
Output is correct |
51 |
Correct |
89 ms |
25368 KB |
Output is correct |
52 |
Correct |
77 ms |
36420 KB |
Output is correct |
53 |
Correct |
129 ms |
29904 KB |
Output is correct |
54 |
Correct |
77 ms |
25476 KB |
Output is correct |
55 |
Correct |
90 ms |
25120 KB |
Output is correct |
56 |
Correct |
80 ms |
36388 KB |
Output is correct |
57 |
Correct |
116 ms |
30720 KB |
Output is correct |
58 |
Correct |
77 ms |
25532 KB |
Output is correct |
59 |
Correct |
98 ms |
25200 KB |
Output is correct |
60 |
Correct |
78 ms |
36424 KB |
Output is correct |
61 |
Correct |
116 ms |
30988 KB |
Output is correct |
62 |
Correct |
84 ms |
25248 KB |
Output is correct |
63 |
Correct |
111 ms |
25268 KB |
Output is correct |
64 |
Correct |
80 ms |
36412 KB |
Output is correct |
65 |
Correct |
143 ms |
30792 KB |
Output is correct |
66 |
Correct |
72 ms |
25452 KB |
Output is correct |
67 |
Correct |
87 ms |
25128 KB |
Output is correct |
68 |
Correct |
86 ms |
36424 KB |
Output is correct |
69 |
Correct |
100 ms |
29256 KB |
Output is correct |
70 |
Correct |
81 ms |
25656 KB |
Output is correct |