#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
const int N = 100000;
int n;
vector<int> g[N+5];
int timer, depth[N+10], sub[N+10];
int up[N + 10][18], bigchild[N+10], chain[N+10];
int pos[N+10];
void dfs(int v, int par){
up[v][0] = par;
sub[v] = 1;
for(int j = 1; j < 18; j++){
up[v][j] = up[up[v][j-1]][j-1];
}
for(int to : g[v]){
if(to == par) continue;
depth[to] = depth[v] + 1;
dfs(to, v);
sub[v]+= sub[to];
if(!bigchild[to] || sub[bigchild[v]] < sub[to]){
bigchild[v] = to;
}
}
}
void decompose(int v, int par, int head){
chain[v] = head;
pos[v] = timer++;
if(bigchild[v]){
decompose(bigchild[v], v, head);
}
for(int to : g[v]){
if(to == par || to == bigchild[v]) continue;
decompose(to, v, to);
}
}
int lca(int a, int b){
if(depth[a] > depth[b]) swap(a, b);
int k = depth[b] - depth[a];
for(int i = 0;i < 18; i++){
if(k & (1<<i)){
b = up[b][i];
}
}
if(a == b) return a;
for(int i = 17; i >= 0; i--){
if(up[a][i] != up[b][i]){
a = up[a][i];
b = up[b][i];
}
}
return up[a][0];
}
long long t[N + 100];
int pref(int i){
int sum = 0;
for(; i > 0; i-= i & -i){
sum+= t[i];
}
return sum;
}
void add(int i, int x){
for(; i <= n; i+= i & -i){
t[i]+= x;
}
}
int sum(int l, int r){
return pref(r) - pref(l-1);
}
long long query(int a, int b){
long long ans = 0;
while(chain[b] != chain[a]){
ans+= sum(pos[chain[b]], pos[b]);
b = up[chain[b]][0];
}
ans+= sum(pos[a], pos[b]);
return ans;
}
long long dp[N+10][2];
vector<vector<array<int, 3> > > transit(N+10);
int last[N+10];
void calc(int v, int par){
long long sum1 = 0;
for(int to : g[v]){
if(to == par) continue;
calc(to, v);
sum1+= max(dp[to][0], dp[to][1]);
}
dp[v][0] = max(dp[v][0], sum1);
dp[v][1] = max(dp[v][1], sum1);
for(auto &[a, b, c] : transit[v]){
long long sumA = query(v, a) + query(v, b) + c + dp[v][0];
dp[v][1] = max(dp[v][1], sumA);
}
add(pos[v], (dp[v][0] - dp[v][1]) - last[pos[v]]);
last[pos[v]] = dp[v][0] - dp[v][1];
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1;i < n; i++){
int a, b; cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
int m; cin >> m;
dfs(1, 1);
for(auto i = 0;i < m; i++){
int a, b, c;
cin >> a >> b >> c;
int lc = lca(a, b);
transit[lc].push_back({a, b, c});
}
timer = 1;
decompose(1, 1, 1);
calc(1, 1);
cout << dp[1][1] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10332 KB |
Output is correct |
2 |
Correct |
3 ms |
10416 KB |
Output is correct |
3 |
Correct |
3 ms |
10332 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
50 ms |
20820 KB |
Output is correct |
6 |
Correct |
41 ms |
29984 KB |
Output is correct |
7 |
Correct |
57 ms |
26804 KB |
Output is correct |
8 |
Correct |
45 ms |
21084 KB |
Output is correct |
9 |
Correct |
88 ms |
24976 KB |
Output is correct |
10 |
Correct |
44 ms |
21216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
10328 KB |
Output is correct |
2 |
Correct |
3 ms |
10332 KB |
Output is correct |
3 |
Correct |
3 ms |
10580 KB |
Output is correct |
4 |
Correct |
78 ms |
32852 KB |
Output is correct |
5 |
Correct |
78 ms |
32852 KB |
Output is correct |
6 |
Correct |
96 ms |
32992 KB |
Output is correct |
7 |
Correct |
76 ms |
32720 KB |
Output is correct |
8 |
Correct |
76 ms |
32844 KB |
Output is correct |
9 |
Correct |
72 ms |
32804 KB |
Output is correct |
10 |
Correct |
91 ms |
32736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
10328 KB |
Output is correct |
2 |
Correct |
3 ms |
10332 KB |
Output is correct |
3 |
Correct |
3 ms |
10580 KB |
Output is correct |
4 |
Correct |
78 ms |
32852 KB |
Output is correct |
5 |
Correct |
78 ms |
32852 KB |
Output is correct |
6 |
Correct |
96 ms |
32992 KB |
Output is correct |
7 |
Correct |
76 ms |
32720 KB |
Output is correct |
8 |
Correct |
76 ms |
32844 KB |
Output is correct |
9 |
Correct |
72 ms |
32804 KB |
Output is correct |
10 |
Correct |
91 ms |
32736 KB |
Output is correct |
11 |
Correct |
9 ms |
11356 KB |
Output is correct |
12 |
Correct |
83 ms |
32740 KB |
Output is correct |
13 |
Correct |
79 ms |
32936 KB |
Output is correct |
14 |
Correct |
71 ms |
32852 KB |
Output is correct |
15 |
Correct |
83 ms |
32852 KB |
Output is correct |
16 |
Correct |
89 ms |
33136 KB |
Output is correct |
17 |
Correct |
77 ms |
32848 KB |
Output is correct |
18 |
Correct |
85 ms |
32860 KB |
Output is correct |
19 |
Correct |
85 ms |
32780 KB |
Output is correct |
20 |
Correct |
92 ms |
32852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
133 ms |
23140 KB |
Output is correct |
2 |
Correct |
72 ms |
33108 KB |
Output is correct |
3 |
Execution timed out |
1012 ms |
29268 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10332 KB |
Output is correct |
2 |
Correct |
3 ms |
10416 KB |
Output is correct |
3 |
Correct |
3 ms |
10332 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
50 ms |
20820 KB |
Output is correct |
6 |
Correct |
41 ms |
29984 KB |
Output is correct |
7 |
Correct |
57 ms |
26804 KB |
Output is correct |
8 |
Correct |
45 ms |
21084 KB |
Output is correct |
9 |
Correct |
88 ms |
24976 KB |
Output is correct |
10 |
Correct |
44 ms |
21216 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
3 ms |
10588 KB |
Output is correct |
13 |
Correct |
3 ms |
10588 KB |
Output is correct |
14 |
Correct |
3 ms |
10588 KB |
Output is correct |
15 |
Correct |
5 ms |
10584 KB |
Output is correct |
16 |
Correct |
4 ms |
10588 KB |
Output is correct |
17 |
Correct |
4 ms |
10448 KB |
Output is correct |
18 |
Correct |
4 ms |
10588 KB |
Output is correct |
19 |
Correct |
3 ms |
10588 KB |
Output is correct |
20 |
Correct |
3 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10332 KB |
Output is correct |
2 |
Correct |
3 ms |
10416 KB |
Output is correct |
3 |
Correct |
3 ms |
10332 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
50 ms |
20820 KB |
Output is correct |
6 |
Correct |
41 ms |
29984 KB |
Output is correct |
7 |
Correct |
57 ms |
26804 KB |
Output is correct |
8 |
Correct |
45 ms |
21084 KB |
Output is correct |
9 |
Correct |
88 ms |
24976 KB |
Output is correct |
10 |
Correct |
44 ms |
21216 KB |
Output is correct |
11 |
Correct |
4 ms |
10328 KB |
Output is correct |
12 |
Correct |
3 ms |
10332 KB |
Output is correct |
13 |
Correct |
3 ms |
10580 KB |
Output is correct |
14 |
Correct |
78 ms |
32852 KB |
Output is correct |
15 |
Correct |
78 ms |
32852 KB |
Output is correct |
16 |
Correct |
96 ms |
32992 KB |
Output is correct |
17 |
Correct |
76 ms |
32720 KB |
Output is correct |
18 |
Correct |
76 ms |
32844 KB |
Output is correct |
19 |
Correct |
72 ms |
32804 KB |
Output is correct |
20 |
Correct |
91 ms |
32736 KB |
Output is correct |
21 |
Correct |
9 ms |
11356 KB |
Output is correct |
22 |
Correct |
83 ms |
32740 KB |
Output is correct |
23 |
Correct |
79 ms |
32936 KB |
Output is correct |
24 |
Correct |
71 ms |
32852 KB |
Output is correct |
25 |
Correct |
83 ms |
32852 KB |
Output is correct |
26 |
Correct |
89 ms |
33136 KB |
Output is correct |
27 |
Correct |
77 ms |
32848 KB |
Output is correct |
28 |
Correct |
85 ms |
32860 KB |
Output is correct |
29 |
Correct |
85 ms |
32780 KB |
Output is correct |
30 |
Correct |
92 ms |
32852 KB |
Output is correct |
31 |
Correct |
133 ms |
23140 KB |
Output is correct |
32 |
Correct |
72 ms |
33108 KB |
Output is correct |
33 |
Execution timed out |
1012 ms |
29268 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |