#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int MAXN = 100000;
const int MXLOG = 18;
vector <int> graf[MAXN+5];
int dp[MAXN+5];
int par[MAXN+5][MXLOG+1];
int tjm;
int in[MAXN+5];
int out[MAXN+5];
int bit[2*MAXN+5];
int niz[2*MAXN+5];
void upd(int x, int val){
while(x <= 2*MAXN){
bit[x] += val;
x += x & -x;
}
}
int bitget(int x){
int res = 0;
while(x){
res += bit[x];
x -= x & -x;
}
return res;
}
int query(int l, int r){
return bitget(r) - bitget(l-1);
}
void dfs_calc(int v, int p){
in[v] = ++tjm;
par[v][0] = p;
for(int j=1; j<=MXLOG; j++) par[v][j] = par[par[v][j-1]][j-1];
for(auto c : graf[v]) if(c != p) dfs_calc(c, v);
out[v] = ++tjm;
}
bool is_parent(int a, int b){
return (a == 0) || (in[a] <= in[b] && out[b] <= out[a]);
}
int lca(int a, int b){
if(is_parent(a, b)) return a;
for(int j=MXLOG; j>=0; j--) if(!is_parent(par[a][j], b)) a = par[a][j];
return par[a][0];
}
struct Option{
int a, b, c;
};
vector <Option> options[MAXN+5];
int prvidole(int a, int b){
for(int j=MXLOG; j>=0; j--) if(!is_parent(par[b][j], a)) b = par[b][j];
return b;
}
void dfs_solve(int v, int p){
for(auto c : graf[v]){
if(c != p){
dfs_solve(c, v);
dp[v] += dp[c];
}
}
for(auto option : options[v]){
int a = option.a;
int b = option.b;
int c = option.c;
int val = c;
if(is_parent(b, a)) swap(a, b);
if(a == v){
val += query(in[v], in[b]);
}
else{
val += query(in[v], in[a]);
int k = prvidole(v, b);
val += query(in[k], in[b]);
}
dp[v] = max(dp[v], val);
}
if(v != 1){
upd(in[p], dp[v]);
upd(in[v], -dp[v]);
upd(out[v], dp[v]);
upd(out[p], -dp[v]);
}
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int n;
cin >> n;
for(int i=1; i<n; i++){
int a, b;
cin >> a >> b;
graf[a].push_back(b);
graf[b].push_back(a);
}
dfs_calc(1, 0);
int m;
cin >> m;
while(m--){
int a, b, c;
cin >> a >> b >> c;
options[lca(a, b)].push_back({a, b, c});
}
dfs_solve(1, 0);
cout << dp[1] << "\n";
return 0;
}
/*
7
1 2
2 3
3 4
1 5
5 6
6 7
3
3 4 1
6 7 1
2 5 1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5196 KB |
Output is correct |
5 |
Correct |
100 ms |
17692 KB |
Output is correct |
6 |
Correct |
62 ms |
27984 KB |
Output is correct |
7 |
Correct |
109 ms |
24732 KB |
Output is correct |
8 |
Correct |
83 ms |
18912 KB |
Output is correct |
9 |
Correct |
102 ms |
22868 KB |
Output is correct |
10 |
Correct |
85 ms |
18820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5196 KB |
Output is correct |
4 |
Correct |
141 ms |
28836 KB |
Output is correct |
5 |
Correct |
131 ms |
28896 KB |
Output is correct |
6 |
Correct |
124 ms |
28832 KB |
Output is correct |
7 |
Correct |
132 ms |
28868 KB |
Output is correct |
8 |
Correct |
132 ms |
28816 KB |
Output is correct |
9 |
Correct |
112 ms |
28872 KB |
Output is correct |
10 |
Correct |
131 ms |
28996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5196 KB |
Output is correct |
4 |
Correct |
141 ms |
28836 KB |
Output is correct |
5 |
Correct |
131 ms |
28896 KB |
Output is correct |
6 |
Correct |
124 ms |
28832 KB |
Output is correct |
7 |
Correct |
132 ms |
28868 KB |
Output is correct |
8 |
Correct |
132 ms |
28816 KB |
Output is correct |
9 |
Correct |
112 ms |
28872 KB |
Output is correct |
10 |
Correct |
131 ms |
28996 KB |
Output is correct |
11 |
Correct |
17 ms |
5756 KB |
Output is correct |
12 |
Correct |
139 ms |
28824 KB |
Output is correct |
13 |
Correct |
133 ms |
28892 KB |
Output is correct |
14 |
Correct |
121 ms |
28824 KB |
Output is correct |
15 |
Correct |
135 ms |
29032 KB |
Output is correct |
16 |
Correct |
124 ms |
28848 KB |
Output is correct |
17 |
Correct |
139 ms |
28992 KB |
Output is correct |
18 |
Correct |
137 ms |
28868 KB |
Output is correct |
19 |
Correct |
131 ms |
28868 KB |
Output is correct |
20 |
Correct |
154 ms |
28928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
166 ms |
19068 KB |
Output is correct |
2 |
Correct |
122 ms |
31272 KB |
Output is correct |
3 |
Correct |
265 ms |
27696 KB |
Output is correct |
4 |
Correct |
166 ms |
21792 KB |
Output is correct |
5 |
Correct |
203 ms |
27076 KB |
Output is correct |
6 |
Correct |
140 ms |
21668 KB |
Output is correct |
7 |
Correct |
229 ms |
26752 KB |
Output is correct |
8 |
Correct |
180 ms |
21884 KB |
Output is correct |
9 |
Correct |
116 ms |
31296 KB |
Output is correct |
10 |
Correct |
233 ms |
25592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5196 KB |
Output is correct |
5 |
Correct |
100 ms |
17692 KB |
Output is correct |
6 |
Correct |
62 ms |
27984 KB |
Output is correct |
7 |
Correct |
109 ms |
24732 KB |
Output is correct |
8 |
Correct |
83 ms |
18912 KB |
Output is correct |
9 |
Correct |
102 ms |
22868 KB |
Output is correct |
10 |
Correct |
85 ms |
18820 KB |
Output is correct |
11 |
Correct |
5 ms |
5160 KB |
Output is correct |
12 |
Correct |
4 ms |
5324 KB |
Output is correct |
13 |
Correct |
4 ms |
5196 KB |
Output is correct |
14 |
Correct |
4 ms |
5216 KB |
Output is correct |
15 |
Correct |
4 ms |
5196 KB |
Output is correct |
16 |
Correct |
4 ms |
5196 KB |
Output is correct |
17 |
Correct |
5 ms |
5196 KB |
Output is correct |
18 |
Correct |
5 ms |
5196 KB |
Output is correct |
19 |
Correct |
5 ms |
5256 KB |
Output is correct |
20 |
Correct |
4 ms |
5196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5196 KB |
Output is correct |
5 |
Correct |
100 ms |
17692 KB |
Output is correct |
6 |
Correct |
62 ms |
27984 KB |
Output is correct |
7 |
Correct |
109 ms |
24732 KB |
Output is correct |
8 |
Correct |
83 ms |
18912 KB |
Output is correct |
9 |
Correct |
102 ms |
22868 KB |
Output is correct |
10 |
Correct |
85 ms |
18820 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
12 |
Correct |
3 ms |
5068 KB |
Output is correct |
13 |
Correct |
4 ms |
5196 KB |
Output is correct |
14 |
Correct |
141 ms |
28836 KB |
Output is correct |
15 |
Correct |
131 ms |
28896 KB |
Output is correct |
16 |
Correct |
124 ms |
28832 KB |
Output is correct |
17 |
Correct |
132 ms |
28868 KB |
Output is correct |
18 |
Correct |
132 ms |
28816 KB |
Output is correct |
19 |
Correct |
112 ms |
28872 KB |
Output is correct |
20 |
Correct |
131 ms |
28996 KB |
Output is correct |
21 |
Correct |
17 ms |
5756 KB |
Output is correct |
22 |
Correct |
139 ms |
28824 KB |
Output is correct |
23 |
Correct |
133 ms |
28892 KB |
Output is correct |
24 |
Correct |
121 ms |
28824 KB |
Output is correct |
25 |
Correct |
135 ms |
29032 KB |
Output is correct |
26 |
Correct |
124 ms |
28848 KB |
Output is correct |
27 |
Correct |
139 ms |
28992 KB |
Output is correct |
28 |
Correct |
137 ms |
28868 KB |
Output is correct |
29 |
Correct |
131 ms |
28868 KB |
Output is correct |
30 |
Correct |
154 ms |
28928 KB |
Output is correct |
31 |
Correct |
166 ms |
19068 KB |
Output is correct |
32 |
Correct |
122 ms |
31272 KB |
Output is correct |
33 |
Correct |
265 ms |
27696 KB |
Output is correct |
34 |
Correct |
166 ms |
21792 KB |
Output is correct |
35 |
Correct |
203 ms |
27076 KB |
Output is correct |
36 |
Correct |
140 ms |
21668 KB |
Output is correct |
37 |
Correct |
229 ms |
26752 KB |
Output is correct |
38 |
Correct |
180 ms |
21884 KB |
Output is correct |
39 |
Correct |
116 ms |
31296 KB |
Output is correct |
40 |
Correct |
233 ms |
25592 KB |
Output is correct |
41 |
Correct |
5 ms |
5160 KB |
Output is correct |
42 |
Correct |
4 ms |
5324 KB |
Output is correct |
43 |
Correct |
4 ms |
5196 KB |
Output is correct |
44 |
Correct |
4 ms |
5216 KB |
Output is correct |
45 |
Correct |
4 ms |
5196 KB |
Output is correct |
46 |
Correct |
4 ms |
5196 KB |
Output is correct |
47 |
Correct |
5 ms |
5196 KB |
Output is correct |
48 |
Correct |
5 ms |
5196 KB |
Output is correct |
49 |
Correct |
5 ms |
5256 KB |
Output is correct |
50 |
Correct |
4 ms |
5196 KB |
Output is correct |
51 |
Correct |
196 ms |
22088 KB |
Output is correct |
52 |
Correct |
149 ms |
31672 KB |
Output is correct |
53 |
Correct |
252 ms |
25976 KB |
Output is correct |
54 |
Correct |
150 ms |
22176 KB |
Output is correct |
55 |
Correct |
193 ms |
21888 KB |
Output is correct |
56 |
Correct |
133 ms |
31632 KB |
Output is correct |
57 |
Correct |
200 ms |
26844 KB |
Output is correct |
58 |
Correct |
139 ms |
21808 KB |
Output is correct |
59 |
Correct |
188 ms |
22072 KB |
Output is correct |
60 |
Correct |
131 ms |
31552 KB |
Output is correct |
61 |
Correct |
210 ms |
26928 KB |
Output is correct |
62 |
Correct |
142 ms |
21664 KB |
Output is correct |
63 |
Correct |
175 ms |
21676 KB |
Output is correct |
64 |
Correct |
139 ms |
31556 KB |
Output is correct |
65 |
Correct |
248 ms |
26700 KB |
Output is correct |
66 |
Correct |
138 ms |
22016 KB |
Output is correct |
67 |
Correct |
184 ms |
21940 KB |
Output is correct |
68 |
Correct |
167 ms |
31556 KB |
Output is correct |
69 |
Correct |
201 ms |
25412 KB |
Output is correct |
70 |
Correct |
153 ms |
22076 KB |
Output is correct |