#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m;
vector<int>g[N];
int tab[18][N];
int depth[N];
int pos=0;
int in[N];
int out[N];
void DFS(int u,int par) {
pos++;
in[u]=pos;
for(auto v:g[u])
if(v!=par) {
tab[0][v]=u;
depth[v]=depth[u]+1;
DFS(v,u);
}
out[u]=pos;
}
void init() {
depth[1]=1;
DFS(1,0);
for(int i=1; i<=17; i++)
for(int j=1; j<=n; j++)
tab[i][j]=tab[i-1][tab[i-1][j]];
}
int LCA(int u,int v) {
if(depth[u]<depth[v])swap(u,v);
for(int i=17; i>=0; i--)if(depth[tab[i][u]]>=depth[v])u=tab[i][u];
if(u==v)return u;
for(int i=17; i>=0; i--)if(tab[i][u]!=tab[i][v]) {
v=tab[i][v];
u=tab[i][u];
}
return tab[0][u];
}
long long dp[N];
struct event {
int u,v,c;
};
vector<event>save[N];
long long aib[N];
void add(int i, int x) {
while (i <N) {
aib[i] += x;
i += i & (-i);
}
}
long long get(int i) {
int sol = 0;
while (i) {
sol += aib[i];
i -= i & (-i);
}
return sol;
}
long long sum[N];
void DFS_ans(int u,int par) {
for(auto v:g[u])
if(v!=par) {
DFS_ans(v,u);
sum[u]+=dp[v];
}
dp[u]=sum[u];
for(auto v:save[u]) {
dp[u]=max(dp[u],get(in[v.u])+get(in[v.v])+v.c+sum[u]);
}
add(in[u],sum[u]-dp[u]);
add(out[u]+1,dp[u]-sum[u]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1; i<n; i++) {
int u,v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
init();
cin>>m;
while(m--) {
int u,v,c;
cin>>u>>v>>c;
save[LCA(u,v)].push_back({u,v,c});
}
DFS_ans(1,0);
cout<<dp[1];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5152 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
45 ms |
20004 KB |
Output is correct |
6 |
Correct |
33 ms |
29216 KB |
Output is correct |
7 |
Correct |
48 ms |
25908 KB |
Output is correct |
8 |
Correct |
38 ms |
19536 KB |
Output is correct |
9 |
Correct |
65 ms |
24028 KB |
Output is correct |
10 |
Correct |
46 ms |
19556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5296 KB |
Output is correct |
4 |
Correct |
94 ms |
32520 KB |
Output is correct |
5 |
Correct |
99 ms |
32456 KB |
Output is correct |
6 |
Correct |
99 ms |
32468 KB |
Output is correct |
7 |
Correct |
109 ms |
32536 KB |
Output is correct |
8 |
Correct |
99 ms |
32460 KB |
Output is correct |
9 |
Correct |
96 ms |
32500 KB |
Output is correct |
10 |
Correct |
95 ms |
32520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5296 KB |
Output is correct |
4 |
Correct |
94 ms |
32520 KB |
Output is correct |
5 |
Correct |
99 ms |
32456 KB |
Output is correct |
6 |
Correct |
99 ms |
32468 KB |
Output is correct |
7 |
Correct |
109 ms |
32536 KB |
Output is correct |
8 |
Correct |
99 ms |
32460 KB |
Output is correct |
9 |
Correct |
96 ms |
32500 KB |
Output is correct |
10 |
Correct |
95 ms |
32520 KB |
Output is correct |
11 |
Correct |
10 ms |
6100 KB |
Output is correct |
12 |
Correct |
99 ms |
32784 KB |
Output is correct |
13 |
Correct |
104 ms |
32832 KB |
Output is correct |
14 |
Correct |
97 ms |
32740 KB |
Output is correct |
15 |
Correct |
97 ms |
32844 KB |
Output is correct |
16 |
Correct |
96 ms |
32800 KB |
Output is correct |
17 |
Correct |
102 ms |
32744 KB |
Output is correct |
18 |
Correct |
103 ms |
32836 KB |
Output is correct |
19 |
Correct |
120 ms |
32840 KB |
Output is correct |
20 |
Correct |
99 ms |
32820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
141 ms |
22744 KB |
Output is correct |
2 |
Correct |
96 ms |
32512 KB |
Output is correct |
3 |
Correct |
154 ms |
28892 KB |
Output is correct |
4 |
Correct |
94 ms |
22448 KB |
Output is correct |
5 |
Correct |
132 ms |
28212 KB |
Output is correct |
6 |
Correct |
93 ms |
22396 KB |
Output is correct |
7 |
Correct |
169 ms |
27916 KB |
Output is correct |
8 |
Correct |
123 ms |
22968 KB |
Output is correct |
9 |
Correct |
94 ms |
32524 KB |
Output is correct |
10 |
Correct |
195 ms |
26796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5152 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
45 ms |
20004 KB |
Output is correct |
6 |
Correct |
33 ms |
29216 KB |
Output is correct |
7 |
Correct |
48 ms |
25908 KB |
Output is correct |
8 |
Correct |
38 ms |
19536 KB |
Output is correct |
9 |
Correct |
65 ms |
24028 KB |
Output is correct |
10 |
Correct |
46 ms |
19556 KB |
Output is correct |
11 |
Correct |
4 ms |
5332 KB |
Output is correct |
12 |
Correct |
3 ms |
5332 KB |
Output is correct |
13 |
Correct |
4 ms |
5300 KB |
Output is correct |
14 |
Correct |
3 ms |
5276 KB |
Output is correct |
15 |
Correct |
3 ms |
5296 KB |
Output is correct |
16 |
Correct |
4 ms |
5360 KB |
Output is correct |
17 |
Correct |
3 ms |
5332 KB |
Output is correct |
18 |
Correct |
3 ms |
5324 KB |
Output is correct |
19 |
Correct |
3 ms |
5332 KB |
Output is correct |
20 |
Correct |
4 ms |
5420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5152 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
45 ms |
20004 KB |
Output is correct |
6 |
Correct |
33 ms |
29216 KB |
Output is correct |
7 |
Correct |
48 ms |
25908 KB |
Output is correct |
8 |
Correct |
38 ms |
19536 KB |
Output is correct |
9 |
Correct |
65 ms |
24028 KB |
Output is correct |
10 |
Correct |
46 ms |
19556 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5296 KB |
Output is correct |
14 |
Correct |
94 ms |
32520 KB |
Output is correct |
15 |
Correct |
99 ms |
32456 KB |
Output is correct |
16 |
Correct |
99 ms |
32468 KB |
Output is correct |
17 |
Correct |
109 ms |
32536 KB |
Output is correct |
18 |
Correct |
99 ms |
32460 KB |
Output is correct |
19 |
Correct |
96 ms |
32500 KB |
Output is correct |
20 |
Correct |
95 ms |
32520 KB |
Output is correct |
21 |
Correct |
10 ms |
6100 KB |
Output is correct |
22 |
Correct |
99 ms |
32784 KB |
Output is correct |
23 |
Correct |
104 ms |
32832 KB |
Output is correct |
24 |
Correct |
97 ms |
32740 KB |
Output is correct |
25 |
Correct |
97 ms |
32844 KB |
Output is correct |
26 |
Correct |
96 ms |
32800 KB |
Output is correct |
27 |
Correct |
102 ms |
32744 KB |
Output is correct |
28 |
Correct |
103 ms |
32836 KB |
Output is correct |
29 |
Correct |
120 ms |
32840 KB |
Output is correct |
30 |
Correct |
99 ms |
32820 KB |
Output is correct |
31 |
Correct |
141 ms |
22744 KB |
Output is correct |
32 |
Correct |
96 ms |
32512 KB |
Output is correct |
33 |
Correct |
154 ms |
28892 KB |
Output is correct |
34 |
Correct |
94 ms |
22448 KB |
Output is correct |
35 |
Correct |
132 ms |
28212 KB |
Output is correct |
36 |
Correct |
93 ms |
22396 KB |
Output is correct |
37 |
Correct |
169 ms |
27916 KB |
Output is correct |
38 |
Correct |
123 ms |
22968 KB |
Output is correct |
39 |
Correct |
94 ms |
32524 KB |
Output is correct |
40 |
Correct |
195 ms |
26796 KB |
Output is correct |
41 |
Correct |
4 ms |
5332 KB |
Output is correct |
42 |
Correct |
3 ms |
5332 KB |
Output is correct |
43 |
Correct |
4 ms |
5300 KB |
Output is correct |
44 |
Correct |
3 ms |
5276 KB |
Output is correct |
45 |
Correct |
3 ms |
5296 KB |
Output is correct |
46 |
Correct |
4 ms |
5360 KB |
Output is correct |
47 |
Correct |
3 ms |
5332 KB |
Output is correct |
48 |
Correct |
3 ms |
5324 KB |
Output is correct |
49 |
Correct |
3 ms |
5332 KB |
Output is correct |
50 |
Correct |
4 ms |
5420 KB |
Output is correct |
51 |
Correct |
182 ms |
23260 KB |
Output is correct |
52 |
Correct |
100 ms |
32752 KB |
Output is correct |
53 |
Correct |
137 ms |
27224 KB |
Output is correct |
54 |
Correct |
116 ms |
22808 KB |
Output is correct |
55 |
Correct |
116 ms |
23036 KB |
Output is correct |
56 |
Correct |
109 ms |
32740 KB |
Output is correct |
57 |
Correct |
169 ms |
27984 KB |
Output is correct |
58 |
Correct |
94 ms |
22660 KB |
Output is correct |
59 |
Correct |
130 ms |
23312 KB |
Output is correct |
60 |
Correct |
97 ms |
32732 KB |
Output is correct |
61 |
Correct |
151 ms |
28044 KB |
Output is correct |
62 |
Correct |
90 ms |
22436 KB |
Output is correct |
63 |
Correct |
145 ms |
22872 KB |
Output is correct |
64 |
Correct |
98 ms |
32840 KB |
Output is correct |
65 |
Correct |
139 ms |
27884 KB |
Output is correct |
66 |
Correct |
96 ms |
22728 KB |
Output is correct |
67 |
Correct |
161 ms |
23028 KB |
Output is correct |
68 |
Correct |
97 ms |
32816 KB |
Output is correct |
69 |
Correct |
135 ms |
26596 KB |
Output is correct |
70 |
Correct |
90 ms |
22828 KB |
Output is correct |