#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
int N, M, num;
int dfn[101010], dep[101010], sz[101010];
int P[101010][20], H[101010];
vector<int> adj[101010], U[101010], V[101010], W[101010];
struct BIT{
LL T[101010];
void upd(int k, LL v){for(;k<=N;k+=k&-k)T[k]+=v;}
LL read(int k){LL r=0; for(;k;k^=k&-k)r+=T[k]; return r;}
LL sum(int l, int r){return read(r)-read(l-1);}
} T1, T2;
int dfs(int u, int p){
P[u][0] = p, sz[u] = 1;
for (int i=1; i<=18; i++) P[u][i] = P[P[u][i-1]][i-1];
for (int i=0; i<adj[u].size(); i++){
int v = adj[u][i];
if (v == p){
swap(adj[u][i], adj[u].back());
adj[u].pop_back(), i--;
continue;
}
dep[v] = dep[u]+1;
sz[u] += dfs(v, u);
if (sz[adj[u][0]]<sz[v]) swap(adj[u][0], adj[u][i]);
}
return sz[u];
}
void HLD(int u){
dfn[u] = ++num;
for (int v : adj[u]){
if (dfn[u] == num) H[v] = H[u];
else H[v] = v;
HLD(v);
}
}
int LCA(int u, int v){
if (dep[u] > dep[v]) swap(u, v);
for (int i=18; i>=0; i--) if (dep[u] <= dep[v]-(1<<i)) v = P[v][i];
if (u == v) return u;
for (int i=18; i>=0; i--) if (P[u][i] != P[v][i]) u=P[u][i], v=P[v][i];
return P[u][0];
}
int kp(int u, int k){for (int i=0; i<=18; i++) if (k&(1<<i)) u=P[u][i]; return u;}
LL asum1(int u, int v){
LL r=0;
while (H[v]!=H[u]) r += T1.sum(dfn[H[v]], dfn[v]), v=P[H[v]][0];
r += T1.sum(dfn[u], dfn[v]);
return r;
}
LL asum2(int u, int v){
LL r=0;
while (H[v]!=H[u]) r += T2.sum(dfn[H[v]], dfn[v]), v=P[H[v]][0];
r += T2.sum(dfn[u], dfn[v]);
return r;
}
LL dp(int u){
for (int v : adj[u]) dp(v);
LL ret=T1.sum(dfn[u],dfn[u]);
for (int i=0; i<U[u].size(); i++){
int x=U[u][i], y=V[u][i], w=W[u][i];
LL s = -T1.sum(dfn[u],dfn[u])+w;
s += asum1(u, x)+asum1(u, y);
if (u != x) s -= asum2(kp(x, dep[x]-dep[u]-1), x);
if (u != y) s -= asum2(kp(y, dep[y]-dep[u]-1), y);
ret = max(ret, s);
}
T2.upd(dfn[u], ret);
if (u>1) T1.upd(dfn[P[u][0]], ret);
return ret;
}
int main(){
scanf("%d", &N);
for (int i=1; i<N; i++){
int u, v;
scanf("%d %d", &u, &v);
adj[u].pb(v), adj[v].pb(u);
}
dfs(1, 0);
HLD(1);
scanf("%d", &M);
while (M--){
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
int lca = LCA(u, v);
U[lca].pb(u), V[lca].pb(v), W[lca].pb(w);
}
printf("%lld\n", dp(1));
return 0;
}
Compilation message
election_campaign.cpp: In function 'int dfs(int, int)':
election_campaign.cpp:22:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int i=0; i<adj[u].size(); i++){
| ~^~~~~~~~~~~~~~
election_campaign.cpp: In function 'LL dp(int)':
election_campaign.cpp:72:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i=0; i<U[u].size(); i++){
| ~^~~~~~~~~~~~
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:86:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
86 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
election_campaign.cpp:89:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
89 | scanf("%d %d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~~
election_campaign.cpp:94:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
94 | scanf("%d", &M);
| ~~~~~^~~~~~~~~~
election_campaign.cpp:97:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
97 | scanf("%d %d %d", &u, &v, &w);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9856 KB |
Output is correct |
2 |
Correct |
7 ms |
9856 KB |
Output is correct |
3 |
Correct |
7 ms |
9856 KB |
Output is correct |
4 |
Correct |
7 ms |
9984 KB |
Output is correct |
5 |
Correct |
170 ms |
25208 KB |
Output is correct |
6 |
Correct |
81 ms |
35960 KB |
Output is correct |
7 |
Correct |
174 ms |
32248 KB |
Output is correct |
8 |
Correct |
121 ms |
24940 KB |
Output is correct |
9 |
Correct |
160 ms |
30092 KB |
Output is correct |
10 |
Correct |
116 ms |
24816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9872 KB |
Output is correct |
2 |
Correct |
8 ms |
9856 KB |
Output is correct |
3 |
Correct |
8 ms |
10112 KB |
Output is correct |
4 |
Correct |
265 ms |
40568 KB |
Output is correct |
5 |
Correct |
283 ms |
40544 KB |
Output is correct |
6 |
Correct |
231 ms |
40824 KB |
Output is correct |
7 |
Correct |
286 ms |
40616 KB |
Output is correct |
8 |
Correct |
291 ms |
40688 KB |
Output is correct |
9 |
Correct |
224 ms |
40696 KB |
Output is correct |
10 |
Correct |
257 ms |
40568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9872 KB |
Output is correct |
2 |
Correct |
8 ms |
9856 KB |
Output is correct |
3 |
Correct |
8 ms |
10112 KB |
Output is correct |
4 |
Correct |
265 ms |
40568 KB |
Output is correct |
5 |
Correct |
283 ms |
40544 KB |
Output is correct |
6 |
Correct |
231 ms |
40824 KB |
Output is correct |
7 |
Correct |
286 ms |
40616 KB |
Output is correct |
8 |
Correct |
291 ms |
40688 KB |
Output is correct |
9 |
Correct |
224 ms |
40696 KB |
Output is correct |
10 |
Correct |
257 ms |
40568 KB |
Output is correct |
11 |
Correct |
26 ms |
10872 KB |
Output is correct |
12 |
Correct |
268 ms |
40824 KB |
Output is correct |
13 |
Correct |
262 ms |
40824 KB |
Output is correct |
14 |
Correct |
234 ms |
41040 KB |
Output is correct |
15 |
Correct |
266 ms |
40812 KB |
Output is correct |
16 |
Correct |
238 ms |
40952 KB |
Output is correct |
17 |
Correct |
255 ms |
40924 KB |
Output is correct |
18 |
Correct |
272 ms |
40820 KB |
Output is correct |
19 |
Correct |
239 ms |
40952 KB |
Output is correct |
20 |
Correct |
271 ms |
40952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
356 ms |
28016 KB |
Output is correct |
2 |
Correct |
237 ms |
40824 KB |
Output is correct |
3 |
Correct |
438 ms |
36088 KB |
Output is correct |
4 |
Correct |
232 ms |
27628 KB |
Output is correct |
5 |
Correct |
377 ms |
35840 KB |
Output is correct |
6 |
Correct |
231 ms |
27860 KB |
Output is correct |
7 |
Correct |
453 ms |
35064 KB |
Output is correct |
8 |
Correct |
376 ms |
29584 KB |
Output is correct |
9 |
Correct |
231 ms |
40696 KB |
Output is correct |
10 |
Correct |
466 ms |
33756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9856 KB |
Output is correct |
2 |
Correct |
7 ms |
9856 KB |
Output is correct |
3 |
Correct |
7 ms |
9856 KB |
Output is correct |
4 |
Correct |
7 ms |
9984 KB |
Output is correct |
5 |
Correct |
170 ms |
25208 KB |
Output is correct |
6 |
Correct |
81 ms |
35960 KB |
Output is correct |
7 |
Correct |
174 ms |
32248 KB |
Output is correct |
8 |
Correct |
121 ms |
24940 KB |
Output is correct |
9 |
Correct |
160 ms |
30092 KB |
Output is correct |
10 |
Correct |
116 ms |
24816 KB |
Output is correct |
11 |
Correct |
9 ms |
9984 KB |
Output is correct |
12 |
Correct |
9 ms |
10112 KB |
Output is correct |
13 |
Correct |
9 ms |
10156 KB |
Output is correct |
14 |
Correct |
9 ms |
9984 KB |
Output is correct |
15 |
Correct |
10 ms |
9984 KB |
Output is correct |
16 |
Correct |
8 ms |
10112 KB |
Output is correct |
17 |
Correct |
9 ms |
9984 KB |
Output is correct |
18 |
Correct |
9 ms |
10112 KB |
Output is correct |
19 |
Correct |
9 ms |
9984 KB |
Output is correct |
20 |
Correct |
9 ms |
10112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9856 KB |
Output is correct |
2 |
Correct |
7 ms |
9856 KB |
Output is correct |
3 |
Correct |
7 ms |
9856 KB |
Output is correct |
4 |
Correct |
7 ms |
9984 KB |
Output is correct |
5 |
Correct |
170 ms |
25208 KB |
Output is correct |
6 |
Correct |
81 ms |
35960 KB |
Output is correct |
7 |
Correct |
174 ms |
32248 KB |
Output is correct |
8 |
Correct |
121 ms |
24940 KB |
Output is correct |
9 |
Correct |
160 ms |
30092 KB |
Output is correct |
10 |
Correct |
116 ms |
24816 KB |
Output is correct |
11 |
Correct |
7 ms |
9872 KB |
Output is correct |
12 |
Correct |
8 ms |
9856 KB |
Output is correct |
13 |
Correct |
8 ms |
10112 KB |
Output is correct |
14 |
Correct |
265 ms |
40568 KB |
Output is correct |
15 |
Correct |
283 ms |
40544 KB |
Output is correct |
16 |
Correct |
231 ms |
40824 KB |
Output is correct |
17 |
Correct |
286 ms |
40616 KB |
Output is correct |
18 |
Correct |
291 ms |
40688 KB |
Output is correct |
19 |
Correct |
224 ms |
40696 KB |
Output is correct |
20 |
Correct |
257 ms |
40568 KB |
Output is correct |
21 |
Correct |
26 ms |
10872 KB |
Output is correct |
22 |
Correct |
268 ms |
40824 KB |
Output is correct |
23 |
Correct |
262 ms |
40824 KB |
Output is correct |
24 |
Correct |
234 ms |
41040 KB |
Output is correct |
25 |
Correct |
266 ms |
40812 KB |
Output is correct |
26 |
Correct |
238 ms |
40952 KB |
Output is correct |
27 |
Correct |
255 ms |
40924 KB |
Output is correct |
28 |
Correct |
272 ms |
40820 KB |
Output is correct |
29 |
Correct |
239 ms |
40952 KB |
Output is correct |
30 |
Correct |
271 ms |
40952 KB |
Output is correct |
31 |
Correct |
356 ms |
28016 KB |
Output is correct |
32 |
Correct |
237 ms |
40824 KB |
Output is correct |
33 |
Correct |
438 ms |
36088 KB |
Output is correct |
34 |
Correct |
232 ms |
27628 KB |
Output is correct |
35 |
Correct |
377 ms |
35840 KB |
Output is correct |
36 |
Correct |
231 ms |
27860 KB |
Output is correct |
37 |
Correct |
453 ms |
35064 KB |
Output is correct |
38 |
Correct |
376 ms |
29584 KB |
Output is correct |
39 |
Correct |
231 ms |
40696 KB |
Output is correct |
40 |
Correct |
466 ms |
33756 KB |
Output is correct |
41 |
Correct |
9 ms |
9984 KB |
Output is correct |
42 |
Correct |
9 ms |
10112 KB |
Output is correct |
43 |
Correct |
9 ms |
10156 KB |
Output is correct |
44 |
Correct |
9 ms |
9984 KB |
Output is correct |
45 |
Correct |
10 ms |
9984 KB |
Output is correct |
46 |
Correct |
8 ms |
10112 KB |
Output is correct |
47 |
Correct |
9 ms |
9984 KB |
Output is correct |
48 |
Correct |
9 ms |
10112 KB |
Output is correct |
49 |
Correct |
9 ms |
9984 KB |
Output is correct |
50 |
Correct |
9 ms |
10112 KB |
Output is correct |
51 |
Correct |
356 ms |
29744 KB |
Output is correct |
52 |
Correct |
257 ms |
40856 KB |
Output is correct |
53 |
Correct |
455 ms |
34112 KB |
Output is correct |
54 |
Correct |
209 ms |
28088 KB |
Output is correct |
55 |
Correct |
369 ms |
28296 KB |
Output is correct |
56 |
Correct |
251 ms |
40824 KB |
Output is correct |
57 |
Correct |
374 ms |
35540 KB |
Output is correct |
58 |
Correct |
216 ms |
27948 KB |
Output is correct |
59 |
Correct |
338 ms |
29708 KB |
Output is correct |
60 |
Correct |
244 ms |
40900 KB |
Output is correct |
61 |
Correct |
357 ms |
35704 KB |
Output is correct |
62 |
Correct |
219 ms |
28136 KB |
Output is correct |
63 |
Correct |
401 ms |
28400 KB |
Output is correct |
64 |
Correct |
283 ms |
40824 KB |
Output is correct |
65 |
Correct |
480 ms |
35060 KB |
Output is correct |
66 |
Correct |
202 ms |
28024 KB |
Output is correct |
67 |
Correct |
363 ms |
28528 KB |
Output is correct |
68 |
Correct |
246 ms |
40824 KB |
Output is correct |
69 |
Correct |
346 ms |
33920 KB |
Output is correct |
70 |
Correct |
206 ms |
28024 KB |
Output is correct |