#include <bits/stdc++.h>
using namespace std;
#define int long long
const int modulo = 1e9+7;
int tim[100001];
int out[100001];
int par[100001][17];
int DP[100001][2];
int depth[100001];
int tree[100001];
vector<int> adj[100001];
vector<tuple<int,int,int>> paths[100001];
int c;
void add(int x,int k){
while(x<=100000){
tree[x]+=k;
x+=x&-x;
}
}
int getsum(int x){
int ans = 0;
while(x){
ans+=tree[x];
x-=x&-x;
}
return ans;
}
void dfs(int x,int p,int dep){
par[x][0] = p;
tim[x] = ++c;
depth[x] = dep;
for(int&i:adj[x])if(i!=p)dfs(i,x,dep+1);
out[x] = c+1;
}
void dfs2(int x,int p){
for(int&i:adj[x]){
if(i==p)continue;
dfs2(i,x);
DP[x][0]+=DP[i][1];
}
DP[x][1]=DP[x][0];
for(auto[end1,end2,cost]:paths[x]){
DP[x][1] = max(DP[x][1],DP[x][0]+getsum(tim[end1])+getsum(tim[end2])+cost);
}
add(tim[x],DP[x][0]-DP[x][1]);
add(out[x],DP[x][1]-DP[x][0]);
}
int lca(int a,int b){
if(depth[a]>depth[b])swap(a,b);
int target = depth[b]-depth[a];
for(int bit=0;bit<17;bit++)if(target&(1<<bit))b=par[b][bit];
for(int bit=16;bit>=0;bit--){
if(par[a][bit]!=par[b][bit]){
a = par[a][bit];
b = par[b][bit];
}
}
if(a==b)return a;
return par[a][0];
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for(int i=1;i<n;i++){
int a,b;cin>>a>>b;
adj[a].emplace_back(b);
adj[b].emplace_back(a);
}
dfs(1,0,1);
for(int bit=1;bit<=16;bit++){
for(int i=1;i<=n;i++){
par[i][bit] = par[par[i][bit-1]][bit-1];
}
}
int m;
cin >> m;
for(int i=1;i<=m;i++){
int a,b,c;cin>>a>>b>>c;
paths[lca(a,b)].emplace_back(a,b,c);
}
dfs2(1,0);
cout << DP[1][1] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
66 ms |
27988 KB |
Output is correct |
6 |
Correct |
39 ms |
35164 KB |
Output is correct |
7 |
Correct |
58 ms |
32848 KB |
Output is correct |
8 |
Correct |
44 ms |
28372 KB |
Output is correct |
9 |
Correct |
55 ms |
31372 KB |
Output is correct |
10 |
Correct |
53 ms |
28348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
96 ms |
40020 KB |
Output is correct |
5 |
Correct |
83 ms |
39868 KB |
Output is correct |
6 |
Correct |
87 ms |
40012 KB |
Output is correct |
7 |
Correct |
105 ms |
40000 KB |
Output is correct |
8 |
Correct |
91 ms |
40180 KB |
Output is correct |
9 |
Correct |
82 ms |
39952 KB |
Output is correct |
10 |
Correct |
93 ms |
39964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
96 ms |
40020 KB |
Output is correct |
5 |
Correct |
83 ms |
39868 KB |
Output is correct |
6 |
Correct |
87 ms |
40012 KB |
Output is correct |
7 |
Correct |
105 ms |
40000 KB |
Output is correct |
8 |
Correct |
91 ms |
40180 KB |
Output is correct |
9 |
Correct |
82 ms |
39952 KB |
Output is correct |
10 |
Correct |
93 ms |
39964 KB |
Output is correct |
11 |
Correct |
10 ms |
10072 KB |
Output is correct |
12 |
Correct |
108 ms |
40332 KB |
Output is correct |
13 |
Correct |
108 ms |
40212 KB |
Output is correct |
14 |
Correct |
99 ms |
40404 KB |
Output is correct |
15 |
Correct |
95 ms |
40144 KB |
Output is correct |
16 |
Correct |
83 ms |
40276 KB |
Output is correct |
17 |
Correct |
92 ms |
40788 KB |
Output is correct |
18 |
Correct |
109 ms |
40372 KB |
Output is correct |
19 |
Correct |
81 ms |
40304 KB |
Output is correct |
20 |
Correct |
100 ms |
40168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
113 ms |
32460 KB |
Output is correct |
2 |
Correct |
80 ms |
39952 KB |
Output is correct |
3 |
Correct |
155 ms |
37176 KB |
Output is correct |
4 |
Correct |
72 ms |
32888 KB |
Output is correct |
5 |
Correct |
130 ms |
36644 KB |
Output is correct |
6 |
Correct |
73 ms |
32708 KB |
Output is correct |
7 |
Correct |
174 ms |
36384 KB |
Output is correct |
8 |
Correct |
105 ms |
32480 KB |
Output is correct |
9 |
Correct |
84 ms |
39916 KB |
Output is correct |
10 |
Correct |
126 ms |
35204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
66 ms |
27988 KB |
Output is correct |
6 |
Correct |
39 ms |
35164 KB |
Output is correct |
7 |
Correct |
58 ms |
32848 KB |
Output is correct |
8 |
Correct |
44 ms |
28372 KB |
Output is correct |
9 |
Correct |
55 ms |
31372 KB |
Output is correct |
10 |
Correct |
53 ms |
28348 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
3 ms |
8812 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
3 ms |
8796 KB |
Output is correct |
15 |
Correct |
3 ms |
8808 KB |
Output is correct |
16 |
Correct |
2 ms |
9048 KB |
Output is correct |
17 |
Correct |
2 ms |
8852 KB |
Output is correct |
18 |
Correct |
3 ms |
9052 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
8984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
66 ms |
27988 KB |
Output is correct |
6 |
Correct |
39 ms |
35164 KB |
Output is correct |
7 |
Correct |
58 ms |
32848 KB |
Output is correct |
8 |
Correct |
44 ms |
28372 KB |
Output is correct |
9 |
Correct |
55 ms |
31372 KB |
Output is correct |
10 |
Correct |
53 ms |
28348 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
3 ms |
8796 KB |
Output is correct |
14 |
Correct |
96 ms |
40020 KB |
Output is correct |
15 |
Correct |
83 ms |
39868 KB |
Output is correct |
16 |
Correct |
87 ms |
40012 KB |
Output is correct |
17 |
Correct |
105 ms |
40000 KB |
Output is correct |
18 |
Correct |
91 ms |
40180 KB |
Output is correct |
19 |
Correct |
82 ms |
39952 KB |
Output is correct |
20 |
Correct |
93 ms |
39964 KB |
Output is correct |
21 |
Correct |
10 ms |
10072 KB |
Output is correct |
22 |
Correct |
108 ms |
40332 KB |
Output is correct |
23 |
Correct |
108 ms |
40212 KB |
Output is correct |
24 |
Correct |
99 ms |
40404 KB |
Output is correct |
25 |
Correct |
95 ms |
40144 KB |
Output is correct |
26 |
Correct |
83 ms |
40276 KB |
Output is correct |
27 |
Correct |
92 ms |
40788 KB |
Output is correct |
28 |
Correct |
109 ms |
40372 KB |
Output is correct |
29 |
Correct |
81 ms |
40304 KB |
Output is correct |
30 |
Correct |
100 ms |
40168 KB |
Output is correct |
31 |
Correct |
113 ms |
32460 KB |
Output is correct |
32 |
Correct |
80 ms |
39952 KB |
Output is correct |
33 |
Correct |
155 ms |
37176 KB |
Output is correct |
34 |
Correct |
72 ms |
32888 KB |
Output is correct |
35 |
Correct |
130 ms |
36644 KB |
Output is correct |
36 |
Correct |
73 ms |
32708 KB |
Output is correct |
37 |
Correct |
174 ms |
36384 KB |
Output is correct |
38 |
Correct |
105 ms |
32480 KB |
Output is correct |
39 |
Correct |
84 ms |
39916 KB |
Output is correct |
40 |
Correct |
126 ms |
35204 KB |
Output is correct |
41 |
Correct |
2 ms |
8796 KB |
Output is correct |
42 |
Correct |
3 ms |
8812 KB |
Output is correct |
43 |
Correct |
2 ms |
8796 KB |
Output is correct |
44 |
Correct |
3 ms |
8796 KB |
Output is correct |
45 |
Correct |
3 ms |
8808 KB |
Output is correct |
46 |
Correct |
2 ms |
9048 KB |
Output is correct |
47 |
Correct |
2 ms |
8852 KB |
Output is correct |
48 |
Correct |
3 ms |
9052 KB |
Output is correct |
49 |
Correct |
2 ms |
8796 KB |
Output is correct |
50 |
Correct |
2 ms |
8984 KB |
Output is correct |
51 |
Correct |
135 ms |
32704 KB |
Output is correct |
52 |
Correct |
91 ms |
40168 KB |
Output is correct |
53 |
Correct |
130 ms |
35728 KB |
Output is correct |
54 |
Correct |
81 ms |
33164 KB |
Output is correct |
55 |
Correct |
101 ms |
32552 KB |
Output is correct |
56 |
Correct |
88 ms |
40192 KB |
Output is correct |
57 |
Correct |
117 ms |
36336 KB |
Output is correct |
58 |
Correct |
82 ms |
32976 KB |
Output is correct |
59 |
Correct |
108 ms |
32740 KB |
Output is correct |
60 |
Correct |
86 ms |
40272 KB |
Output is correct |
61 |
Correct |
121 ms |
36584 KB |
Output is correct |
62 |
Correct |
77 ms |
33168 KB |
Output is correct |
63 |
Correct |
106 ms |
32568 KB |
Output is correct |
64 |
Correct |
96 ms |
40280 KB |
Output is correct |
65 |
Correct |
148 ms |
36420 KB |
Output is correct |
66 |
Correct |
73 ms |
32636 KB |
Output is correct |
67 |
Correct |
103 ms |
33216 KB |
Output is correct |
68 |
Correct |
95 ms |
40148 KB |
Output is correct |
69 |
Correct |
123 ms |
35208 KB |
Output is correct |
70 |
Correct |
80 ms |
32956 KB |
Output is correct |