//khodaya khodet komak kon
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define int long long
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int N = 300000 + 10;
const ll MOD = 1000000000 + 7;
const ll INF = 1000000010;
const ll LOG = 19;
int par[N][LOG], n, q, A[N], B[N], C[N], h[N], st[N], ft[N], Time, fen[N], dp[N];
vi G[N], Q[N];
inline void add(int id, int x){
for (; id < N; id += (id & (-id))) fen[id] += x;
}
inline int get(int id){
int res = 0;
for (; id > 0; id -= (id & (-id))) res += fen[id];
return res;
}
void DFS(int v = 1, int p = 0){
st[v] = ++Time;
par[v][0] = p;
for (int i = 1; i < LOG; i++) par[v][i] = par[par[v][i - 1]][i - 1];
for (auto u:G[v]){
if (u == p)continue;
h[u] = h[v] + 1;
DFS(u, v);
}
ft[v] = Time + 1;
}
int getpar(int v, int k){
for (int i = 0; i < LOG; i++){
if (k & (1 << i)) v = par[v][i];
}
return v;
}
int LCA(int v, int u){
if (h[v] < h[u]) swap(v, u);
v = getpar(v, h[v] - h[u]);
if (v == u) return v;
for (int i = LOG - 1; i >= 0; i--){
if (par[v][i] != par[u][i]) v = par[v][i], u = par[u][i];
}
return par[v][0];
}
void DFS2(int v = 1, int p = 0){
int sm = 0;
for (auto u:G[v]){
if (u == p) continue;
DFS2(u, v);
sm += dp[u];
}
dp[v] = sm;
for (auto u:Q[v]){
int lca = LCA(A[u], B[u]);
if (h[A[u]] < h[B[u]]) swap(A[u], B[u]);
if (lca == B[u]){
int vv = getpar(A[u], h[A[u]] - h[v] - 1);
dp[v] = max(dp[v], C[u] + sm - dp[vv] + get(st[A[u]]));
}else{
int vv = getpar(A[u], h[A[u]] - h[v] - 1), uu = getpar(B[u], h[B[u]] - h[v] - 1);
dp[v] = max(dp[v], C[u] + sm - dp[uu] - dp[vv] + get(st[A[u]]) + get(st[B[u]]));
}
}
add(st[v], sm);
add(st[v] + 1, -sm);
for (auto u:G[v]){
if (u == p) continue;
add(st[u], sm - dp[u]);
add(ft[u], -(sm - dp[u]));
}
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 0; i < n - 1; i++){
int v, u;
cin >> v >> u;
G[v].pb(u), G[u].pb(v);
}
DFS();
cin >> q;
for (int i = 1; i <= q; i++){
cin >> A[i] >> B[i] >> C[i];
//cout << LCA(A[i], B[i]) << '\n';
Q[LCA(A[i], B[i])].pb(i);
}
DFS2();
cout << dp[1];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14592 KB |
Output is correct |
2 |
Correct |
11 ms |
14464 KB |
Output is correct |
3 |
Correct |
11 ms |
14592 KB |
Output is correct |
4 |
Correct |
13 ms |
14720 KB |
Output is correct |
5 |
Correct |
138 ms |
37368 KB |
Output is correct |
6 |
Correct |
84 ms |
48528 KB |
Output is correct |
7 |
Correct |
156 ms |
45176 KB |
Output is correct |
8 |
Correct |
128 ms |
38520 KB |
Output is correct |
9 |
Correct |
148 ms |
43000 KB |
Output is correct |
10 |
Correct |
113 ms |
38520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14592 KB |
Output is correct |
2 |
Correct |
9 ms |
14592 KB |
Output is correct |
3 |
Correct |
10 ms |
14848 KB |
Output is correct |
4 |
Correct |
216 ms |
51576 KB |
Output is correct |
5 |
Correct |
220 ms |
51576 KB |
Output is correct |
6 |
Correct |
204 ms |
51576 KB |
Output is correct |
7 |
Correct |
244 ms |
51520 KB |
Output is correct |
8 |
Correct |
286 ms |
51628 KB |
Output is correct |
9 |
Correct |
193 ms |
51576 KB |
Output is correct |
10 |
Correct |
231 ms |
51576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14592 KB |
Output is correct |
2 |
Correct |
9 ms |
14592 KB |
Output is correct |
3 |
Correct |
10 ms |
14848 KB |
Output is correct |
4 |
Correct |
216 ms |
51576 KB |
Output is correct |
5 |
Correct |
220 ms |
51576 KB |
Output is correct |
6 |
Correct |
204 ms |
51576 KB |
Output is correct |
7 |
Correct |
244 ms |
51520 KB |
Output is correct |
8 |
Correct |
286 ms |
51628 KB |
Output is correct |
9 |
Correct |
193 ms |
51576 KB |
Output is correct |
10 |
Correct |
231 ms |
51576 KB |
Output is correct |
11 |
Correct |
24 ms |
16128 KB |
Output is correct |
12 |
Correct |
239 ms |
51576 KB |
Output is correct |
13 |
Correct |
211 ms |
51576 KB |
Output is correct |
14 |
Correct |
183 ms |
51576 KB |
Output is correct |
15 |
Correct |
206 ms |
51704 KB |
Output is correct |
16 |
Correct |
183 ms |
51704 KB |
Output is correct |
17 |
Correct |
214 ms |
51704 KB |
Output is correct |
18 |
Correct |
268 ms |
51704 KB |
Output is correct |
19 |
Correct |
198 ms |
51704 KB |
Output is correct |
20 |
Correct |
243 ms |
51704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
263 ms |
40944 KB |
Output is correct |
2 |
Correct |
187 ms |
53880 KB |
Output is correct |
3 |
Correct |
450 ms |
49912 KB |
Output is correct |
4 |
Correct |
193 ms |
43248 KB |
Output is correct |
5 |
Correct |
337 ms |
49272 KB |
Output is correct |
6 |
Correct |
187 ms |
43180 KB |
Output is correct |
7 |
Correct |
442 ms |
49012 KB |
Output is correct |
8 |
Correct |
253 ms |
43256 KB |
Output is correct |
9 |
Correct |
186 ms |
53752 KB |
Output is correct |
10 |
Correct |
417 ms |
47596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14592 KB |
Output is correct |
2 |
Correct |
11 ms |
14464 KB |
Output is correct |
3 |
Correct |
11 ms |
14592 KB |
Output is correct |
4 |
Correct |
13 ms |
14720 KB |
Output is correct |
5 |
Correct |
138 ms |
37368 KB |
Output is correct |
6 |
Correct |
84 ms |
48528 KB |
Output is correct |
7 |
Correct |
156 ms |
45176 KB |
Output is correct |
8 |
Correct |
128 ms |
38520 KB |
Output is correct |
9 |
Correct |
148 ms |
43000 KB |
Output is correct |
10 |
Correct |
113 ms |
38520 KB |
Output is correct |
11 |
Correct |
11 ms |
14868 KB |
Output is correct |
12 |
Correct |
12 ms |
14976 KB |
Output is correct |
13 |
Correct |
11 ms |
14848 KB |
Output is correct |
14 |
Correct |
13 ms |
14848 KB |
Output is correct |
15 |
Correct |
12 ms |
14848 KB |
Output is correct |
16 |
Correct |
12 ms |
14884 KB |
Output is correct |
17 |
Correct |
11 ms |
14848 KB |
Output is correct |
18 |
Correct |
11 ms |
14848 KB |
Output is correct |
19 |
Correct |
11 ms |
14976 KB |
Output is correct |
20 |
Correct |
12 ms |
14976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14592 KB |
Output is correct |
2 |
Correct |
11 ms |
14464 KB |
Output is correct |
3 |
Correct |
11 ms |
14592 KB |
Output is correct |
4 |
Correct |
13 ms |
14720 KB |
Output is correct |
5 |
Correct |
138 ms |
37368 KB |
Output is correct |
6 |
Correct |
84 ms |
48528 KB |
Output is correct |
7 |
Correct |
156 ms |
45176 KB |
Output is correct |
8 |
Correct |
128 ms |
38520 KB |
Output is correct |
9 |
Correct |
148 ms |
43000 KB |
Output is correct |
10 |
Correct |
113 ms |
38520 KB |
Output is correct |
11 |
Correct |
9 ms |
14592 KB |
Output is correct |
12 |
Correct |
9 ms |
14592 KB |
Output is correct |
13 |
Correct |
10 ms |
14848 KB |
Output is correct |
14 |
Correct |
216 ms |
51576 KB |
Output is correct |
15 |
Correct |
220 ms |
51576 KB |
Output is correct |
16 |
Correct |
204 ms |
51576 KB |
Output is correct |
17 |
Correct |
244 ms |
51520 KB |
Output is correct |
18 |
Correct |
286 ms |
51628 KB |
Output is correct |
19 |
Correct |
193 ms |
51576 KB |
Output is correct |
20 |
Correct |
231 ms |
51576 KB |
Output is correct |
21 |
Correct |
24 ms |
16128 KB |
Output is correct |
22 |
Correct |
239 ms |
51576 KB |
Output is correct |
23 |
Correct |
211 ms |
51576 KB |
Output is correct |
24 |
Correct |
183 ms |
51576 KB |
Output is correct |
25 |
Correct |
206 ms |
51704 KB |
Output is correct |
26 |
Correct |
183 ms |
51704 KB |
Output is correct |
27 |
Correct |
214 ms |
51704 KB |
Output is correct |
28 |
Correct |
268 ms |
51704 KB |
Output is correct |
29 |
Correct |
198 ms |
51704 KB |
Output is correct |
30 |
Correct |
243 ms |
51704 KB |
Output is correct |
31 |
Correct |
263 ms |
40944 KB |
Output is correct |
32 |
Correct |
187 ms |
53880 KB |
Output is correct |
33 |
Correct |
450 ms |
49912 KB |
Output is correct |
34 |
Correct |
193 ms |
43248 KB |
Output is correct |
35 |
Correct |
337 ms |
49272 KB |
Output is correct |
36 |
Correct |
187 ms |
43180 KB |
Output is correct |
37 |
Correct |
442 ms |
49012 KB |
Output is correct |
38 |
Correct |
253 ms |
43256 KB |
Output is correct |
39 |
Correct |
186 ms |
53752 KB |
Output is correct |
40 |
Correct |
417 ms |
47596 KB |
Output is correct |
41 |
Correct |
11 ms |
14868 KB |
Output is correct |
42 |
Correct |
12 ms |
14976 KB |
Output is correct |
43 |
Correct |
11 ms |
14848 KB |
Output is correct |
44 |
Correct |
13 ms |
14848 KB |
Output is correct |
45 |
Correct |
12 ms |
14848 KB |
Output is correct |
46 |
Correct |
12 ms |
14884 KB |
Output is correct |
47 |
Correct |
11 ms |
14848 KB |
Output is correct |
48 |
Correct |
11 ms |
14848 KB |
Output is correct |
49 |
Correct |
11 ms |
14976 KB |
Output is correct |
50 |
Correct |
12 ms |
14976 KB |
Output is correct |
51 |
Correct |
245 ms |
43660 KB |
Output is correct |
52 |
Correct |
203 ms |
54008 KB |
Output is correct |
53 |
Correct |
481 ms |
48112 KB |
Output is correct |
54 |
Correct |
164 ms |
43508 KB |
Output is correct |
55 |
Correct |
345 ms |
43376 KB |
Output is correct |
56 |
Correct |
214 ms |
54008 KB |
Output is correct |
57 |
Correct |
390 ms |
48888 KB |
Output is correct |
58 |
Correct |
204 ms |
43508 KB |
Output is correct |
59 |
Correct |
305 ms |
43512 KB |
Output is correct |
60 |
Correct |
263 ms |
54008 KB |
Output is correct |
61 |
Correct |
334 ms |
49000 KB |
Output is correct |
62 |
Correct |
189 ms |
43256 KB |
Output is correct |
63 |
Correct |
275 ms |
43464 KB |
Output is correct |
64 |
Correct |
243 ms |
54008 KB |
Output is correct |
65 |
Correct |
479 ms |
49004 KB |
Output is correct |
66 |
Correct |
207 ms |
43460 KB |
Output is correct |
67 |
Correct |
279 ms |
43372 KB |
Output is correct |
68 |
Correct |
281 ms |
54008 KB |
Output is correct |
69 |
Correct |
289 ms |
47352 KB |
Output is correct |
70 |
Correct |
185 ms |
43616 KB |
Output is correct |