#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef tuple<ll, ll, ll> tp;
const ll N = 100005, L = 20;
ll n, cc, s[N], e[N], dep[N], par[L][N], dt[N];
vector<ll> adj[N];
vector<tp> v[N];
struct segtree {
ll val[4*N], lim;
void init () {
for(lim = 1; lim <= n; lim <<= 1);
}
void upd (ll S, ll E, ll V) {
S += lim; E += lim;
while(S <= E) {
if(S%2==1) val[S++] += V;
if(E%2==0) val[E--] += V;
S>>=1; E>>=1;
}
}
ll get (ll P) {
ll ret = 0; P += lim;
while(P) {ret += val[P]; P>>=1;}
return ret;
}
} seg;
ll getlca (ll A, ll B) {
if(dep[A] < dep[B]) swap(A, B);
for(ll i=L;i--;) {
if(dep[A]-dep[B]>=(1<<i)) A = par[i][A];
}
if(A == B) return A;
for(ll i=L;i--;) {
if(par[i][A] != par[i][B]) {
A = par[i][A]; B = par[i][B];
}
}
return par[0][A];
}
void calc (ll C, ll P) {
s[C] = ++cc;
dep[C] = dep[P] + 1;
par[0][C] = P;
for(auto &T : adj[C]) {
if(T == P) continue;
calc(T, C);
}
e[C] = cc;
}
void solve (ll C, ll P) {
ll def = 0;
for(auto &T : adj[C]) {
if(T == P) continue;
solve(T, C);
def += dt[T];
}
dt[C] = def;
for(auto &T : v[C]) {
ll A, B, V; tie(A, B, V) = T;
dt[C] = max(dt[C], def+seg.get(s[A])+seg.get(s[B])+V);
}
seg.upd(s[C], e[C], def-dt[C]);
}
int main()
{
scanf("%lld",&n);
for(ll i=1;i<n;i++){
ll A, B;
scanf("%lld%lld",&A,&B);
adj[A].push_back(B);
adj[B].push_back(A);
}
calc(1, 0);
for(ll k=1;k<L;k++) {
for(ll i=1;i<=n;i++) {
par[k][i] = par[k-1][par[k-1][i]];
}
}
ll M;
scanf("%lld",&M);
for(ll i=1;i<=M;i++) {
ll A, B, C;
scanf("%lld%lld%lld",&A,&B,&C);
ll X = getlca(A, B);
v[X].push_back(make_tuple(A, B, C));
}
seg.init();
solve(1, 0);
printf("%lld\n",dt[1]);
}
Compilation message
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:74:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",&n);
^
election_campaign.cpp:77:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld",&A,&B);
^
election_campaign.cpp:88:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",&M);
^
election_campaign.cpp:91:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld%lld",&A,&B,&C);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
28584 KB |
Output is correct |
2 |
Correct |
0 ms |
28584 KB |
Output is correct |
3 |
Correct |
0 ms |
28584 KB |
Output is correct |
4 |
Correct |
3 ms |
28584 KB |
Output is correct |
5 |
Correct |
146 ms |
32544 KB |
Output is correct |
6 |
Correct |
43 ms |
36312 KB |
Output is correct |
7 |
Correct |
136 ms |
35092 KB |
Output is correct |
8 |
Correct |
113 ms |
32996 KB |
Output is correct |
9 |
Correct |
113 ms |
34160 KB |
Output is correct |
10 |
Correct |
96 ms |
33060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
28584 KB |
Output is correct |
2 |
Correct |
0 ms |
28584 KB |
Output is correct |
3 |
Correct |
0 ms |
28584 KB |
Output is correct |
4 |
Correct |
229 ms |
39876 KB |
Output is correct |
5 |
Correct |
249 ms |
39880 KB |
Output is correct |
6 |
Correct |
169 ms |
39876 KB |
Output is correct |
7 |
Correct |
189 ms |
39880 KB |
Output is correct |
8 |
Correct |
146 ms |
39880 KB |
Output is correct |
9 |
Correct |
176 ms |
39880 KB |
Output is correct |
10 |
Correct |
219 ms |
39876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
28584 KB |
Output is correct |
2 |
Correct |
0 ms |
28584 KB |
Output is correct |
3 |
Correct |
0 ms |
28584 KB |
Output is correct |
4 |
Correct |
229 ms |
39876 KB |
Output is correct |
5 |
Correct |
249 ms |
39880 KB |
Output is correct |
6 |
Correct |
169 ms |
39876 KB |
Output is correct |
7 |
Correct |
189 ms |
39880 KB |
Output is correct |
8 |
Correct |
146 ms |
39880 KB |
Output is correct |
9 |
Correct |
176 ms |
39880 KB |
Output is correct |
10 |
Correct |
219 ms |
39876 KB |
Output is correct |
11 |
Correct |
9 ms |
29508 KB |
Output is correct |
12 |
Correct |
206 ms |
39880 KB |
Output is correct |
13 |
Correct |
263 ms |
39876 KB |
Output is correct |
14 |
Correct |
196 ms |
39876 KB |
Output is correct |
15 |
Correct |
226 ms |
39880 KB |
Output is correct |
16 |
Correct |
203 ms |
39880 KB |
Output is correct |
17 |
Correct |
223 ms |
39880 KB |
Output is correct |
18 |
Correct |
236 ms |
39880 KB |
Output is correct |
19 |
Correct |
209 ms |
39880 KB |
Output is correct |
20 |
Correct |
253 ms |
39880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
526 ms |
35960 KB |
Output is correct |
2 |
Correct |
159 ms |
39880 KB |
Output is correct |
3 |
Correct |
526 ms |
38464 KB |
Output is correct |
4 |
Correct |
403 ms |
36916 KB |
Output is correct |
5 |
Correct |
336 ms |
38032 KB |
Output is correct |
6 |
Correct |
386 ms |
37660 KB |
Output is correct |
7 |
Correct |
453 ms |
37764 KB |
Output is correct |
8 |
Correct |
336 ms |
35660 KB |
Output is correct |
9 |
Correct |
206 ms |
39880 KB |
Output is correct |
10 |
Correct |
519 ms |
37544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
28584 KB |
Output is correct |
2 |
Correct |
0 ms |
28584 KB |
Output is correct |
3 |
Correct |
0 ms |
28584 KB |
Output is correct |
4 |
Correct |
3 ms |
28584 KB |
Output is correct |
5 |
Correct |
146 ms |
32544 KB |
Output is correct |
6 |
Correct |
43 ms |
36312 KB |
Output is correct |
7 |
Correct |
136 ms |
35092 KB |
Output is correct |
8 |
Correct |
113 ms |
32996 KB |
Output is correct |
9 |
Correct |
113 ms |
34160 KB |
Output is correct |
10 |
Correct |
96 ms |
33060 KB |
Output is correct |
11 |
Correct |
0 ms |
28720 KB |
Output is correct |
12 |
Correct |
0 ms |
28716 KB |
Output is correct |
13 |
Correct |
3 ms |
28716 KB |
Output is correct |
14 |
Correct |
3 ms |
28716 KB |
Output is correct |
15 |
Correct |
3 ms |
28716 KB |
Output is correct |
16 |
Correct |
3 ms |
28716 KB |
Output is correct |
17 |
Correct |
3 ms |
28716 KB |
Output is correct |
18 |
Correct |
6 ms |
28716 KB |
Output is correct |
19 |
Correct |
3 ms |
28716 KB |
Output is correct |
20 |
Correct |
0 ms |
28716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
28584 KB |
Output is correct |
2 |
Correct |
0 ms |
28584 KB |
Output is correct |
3 |
Correct |
0 ms |
28584 KB |
Output is correct |
4 |
Correct |
3 ms |
28584 KB |
Output is correct |
5 |
Correct |
146 ms |
32544 KB |
Output is correct |
6 |
Correct |
43 ms |
36312 KB |
Output is correct |
7 |
Correct |
136 ms |
35092 KB |
Output is correct |
8 |
Correct |
113 ms |
32996 KB |
Output is correct |
9 |
Correct |
113 ms |
34160 KB |
Output is correct |
10 |
Correct |
96 ms |
33060 KB |
Output is correct |
11 |
Correct |
0 ms |
28584 KB |
Output is correct |
12 |
Correct |
0 ms |
28584 KB |
Output is correct |
13 |
Correct |
0 ms |
28584 KB |
Output is correct |
14 |
Correct |
229 ms |
39876 KB |
Output is correct |
15 |
Correct |
249 ms |
39880 KB |
Output is correct |
16 |
Correct |
169 ms |
39876 KB |
Output is correct |
17 |
Correct |
189 ms |
39880 KB |
Output is correct |
18 |
Correct |
146 ms |
39880 KB |
Output is correct |
19 |
Correct |
176 ms |
39880 KB |
Output is correct |
20 |
Correct |
219 ms |
39876 KB |
Output is correct |
21 |
Correct |
9 ms |
29508 KB |
Output is correct |
22 |
Correct |
206 ms |
39880 KB |
Output is correct |
23 |
Correct |
263 ms |
39876 KB |
Output is correct |
24 |
Correct |
196 ms |
39876 KB |
Output is correct |
25 |
Correct |
226 ms |
39880 KB |
Output is correct |
26 |
Correct |
203 ms |
39880 KB |
Output is correct |
27 |
Correct |
223 ms |
39880 KB |
Output is correct |
28 |
Correct |
236 ms |
39880 KB |
Output is correct |
29 |
Correct |
209 ms |
39880 KB |
Output is correct |
30 |
Correct |
253 ms |
39880 KB |
Output is correct |
31 |
Correct |
526 ms |
35960 KB |
Output is correct |
32 |
Correct |
159 ms |
39880 KB |
Output is correct |
33 |
Correct |
526 ms |
38464 KB |
Output is correct |
34 |
Correct |
403 ms |
36916 KB |
Output is correct |
35 |
Correct |
336 ms |
38032 KB |
Output is correct |
36 |
Correct |
386 ms |
37660 KB |
Output is correct |
37 |
Correct |
453 ms |
37764 KB |
Output is correct |
38 |
Correct |
336 ms |
35660 KB |
Output is correct |
39 |
Correct |
206 ms |
39880 KB |
Output is correct |
40 |
Correct |
519 ms |
37544 KB |
Output is correct |
41 |
Correct |
0 ms |
28720 KB |
Output is correct |
42 |
Correct |
0 ms |
28716 KB |
Output is correct |
43 |
Correct |
3 ms |
28716 KB |
Output is correct |
44 |
Correct |
3 ms |
28716 KB |
Output is correct |
45 |
Correct |
3 ms |
28716 KB |
Output is correct |
46 |
Correct |
3 ms |
28716 KB |
Output is correct |
47 |
Correct |
3 ms |
28716 KB |
Output is correct |
48 |
Correct |
6 ms |
28716 KB |
Output is correct |
49 |
Correct |
3 ms |
28716 KB |
Output is correct |
50 |
Correct |
0 ms |
28716 KB |
Output is correct |
51 |
Correct |
403 ms |
35696 KB |
Output is correct |
52 |
Correct |
259 ms |
39876 KB |
Output is correct |
53 |
Correct |
556 ms |
37752 KB |
Output is correct |
54 |
Correct |
243 ms |
36496 KB |
Output is correct |
55 |
Correct |
403 ms |
36416 KB |
Output is correct |
56 |
Correct |
233 ms |
39884 KB |
Output is correct |
57 |
Correct |
373 ms |
37632 KB |
Output is correct |
58 |
Correct |
356 ms |
36560 KB |
Output is correct |
59 |
Correct |
386 ms |
35664 KB |
Output is correct |
60 |
Correct |
246 ms |
39876 KB |
Output is correct |
61 |
Correct |
363 ms |
37696 KB |
Output is correct |
62 |
Correct |
389 ms |
37780 KB |
Output is correct |
63 |
Correct |
529 ms |
36588 KB |
Output is correct |
64 |
Correct |
256 ms |
39876 KB |
Output is correct |
65 |
Correct |
506 ms |
38756 KB |
Output is correct |
66 |
Correct |
246 ms |
36652 KB |
Output is correct |
67 |
Correct |
533 ms |
37332 KB |
Output is correct |
68 |
Correct |
259 ms |
39876 KB |
Output is correct |
69 |
Correct |
329 ms |
36796 KB |
Output is correct |
70 |
Correct |
396 ms |
37124 KB |
Output is correct |