#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);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
28584 KB |
Output is correct |
2 |
Correct |
3 ms |
28584 KB |
Output is correct |
3 |
Correct |
0 ms |
28584 KB |
Output is correct |
4 |
Correct |
0 ms |
28584 KB |
Output is correct |
5 |
Correct |
129 ms |
32544 KB |
Output is correct |
6 |
Correct |
49 ms |
36316 KB |
Output is correct |
7 |
Correct |
109 ms |
35088 KB |
Output is correct |
8 |
Correct |
79 ms |
32996 KB |
Output is correct |
9 |
Correct |
119 ms |
34156 KB |
Output is correct |
10 |
Correct |
63 ms |
33060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
28584 KB |
Output is correct |
2 |
Correct |
0 ms |
28584 KB |
Output is correct |
3 |
Correct |
3 ms |
28584 KB |
Output is correct |
4 |
Correct |
243 ms |
39876 KB |
Output is correct |
5 |
Correct |
206 ms |
39880 KB |
Output is correct |
6 |
Correct |
159 ms |
39876 KB |
Output is correct |
7 |
Correct |
209 ms |
39880 KB |
Output is correct |
8 |
Correct |
209 ms |
39880 KB |
Output is correct |
9 |
Correct |
123 ms |
39880 KB |
Output is correct |
10 |
Correct |
219 ms |
39876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
28584 KB |
Output is correct |
2 |
Correct |
0 ms |
28584 KB |
Output is correct |
3 |
Correct |
3 ms |
28584 KB |
Output is correct |
4 |
Correct |
243 ms |
39876 KB |
Output is correct |
5 |
Correct |
206 ms |
39880 KB |
Output is correct |
6 |
Correct |
159 ms |
39876 KB |
Output is correct |
7 |
Correct |
209 ms |
39880 KB |
Output is correct |
8 |
Correct |
209 ms |
39880 KB |
Output is correct |
9 |
Correct |
123 ms |
39880 KB |
Output is correct |
10 |
Correct |
219 ms |
39876 KB |
Output is correct |
11 |
Correct |
16 ms |
29508 KB |
Output is correct |
12 |
Correct |
209 ms |
39876 KB |
Output is correct |
13 |
Correct |
269 ms |
39880 KB |
Output is correct |
14 |
Correct |
199 ms |
39880 KB |
Output is correct |
15 |
Correct |
286 ms |
39880 KB |
Output is correct |
16 |
Correct |
219 ms |
39876 KB |
Output is correct |
17 |
Correct |
233 ms |
39880 KB |
Output is correct |
18 |
Correct |
229 ms |
39880 KB |
Output is correct |
19 |
Correct |
179 ms |
39880 KB |
Output is correct |
20 |
Correct |
219 ms |
39876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
486 ms |
35960 KB |
Output is correct |
2 |
Correct |
156 ms |
39876 KB |
Output is correct |
3 |
Correct |
486 ms |
38464 KB |
Output is correct |
4 |
Correct |
346 ms |
36916 KB |
Output is correct |
5 |
Correct |
316 ms |
38036 KB |
Output is correct |
6 |
Correct |
426 ms |
37660 KB |
Output is correct |
7 |
Correct |
399 ms |
37768 KB |
Output is correct |
8 |
Correct |
333 ms |
35660 KB |
Output is correct |
9 |
Correct |
176 ms |
39876 KB |
Output is correct |
10 |
Correct |
516 ms |
37544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
28584 KB |
Output is correct |
2 |
Correct |
3 ms |
28584 KB |
Output is correct |
3 |
Correct |
0 ms |
28584 KB |
Output is correct |
4 |
Correct |
0 ms |
28584 KB |
Output is correct |
5 |
Correct |
129 ms |
32544 KB |
Output is correct |
6 |
Correct |
49 ms |
36316 KB |
Output is correct |
7 |
Correct |
109 ms |
35088 KB |
Output is correct |
8 |
Correct |
79 ms |
32996 KB |
Output is correct |
9 |
Correct |
119 ms |
34156 KB |
Output is correct |
10 |
Correct |
63 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 |
0 ms |
28716 KB |
Output is correct |
14 |
Correct |
0 ms |
28716 KB |
Output is correct |
15 |
Correct |
0 ms |
28716 KB |
Output is correct |
16 |
Correct |
0 ms |
28716 KB |
Output is correct |
17 |
Correct |
0 ms |
28716 KB |
Output is correct |
18 |
Correct |
3 ms |
28716 KB |
Output is correct |
19 |
Correct |
0 ms |
28716 KB |
Output is correct |
20 |
Correct |
3 ms |
28716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
28584 KB |
Output is correct |
2 |
Correct |
3 ms |
28584 KB |
Output is correct |
3 |
Correct |
0 ms |
28584 KB |
Output is correct |
4 |
Correct |
0 ms |
28584 KB |
Output is correct |
5 |
Correct |
129 ms |
32544 KB |
Output is correct |
6 |
Correct |
49 ms |
36316 KB |
Output is correct |
7 |
Correct |
109 ms |
35088 KB |
Output is correct |
8 |
Correct |
79 ms |
32996 KB |
Output is correct |
9 |
Correct |
119 ms |
34156 KB |
Output is correct |
10 |
Correct |
63 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 |
3 ms |
28584 KB |
Output is correct |
14 |
Correct |
243 ms |
39876 KB |
Output is correct |
15 |
Correct |
206 ms |
39880 KB |
Output is correct |
16 |
Correct |
159 ms |
39876 KB |
Output is correct |
17 |
Correct |
209 ms |
39880 KB |
Output is correct |
18 |
Correct |
209 ms |
39880 KB |
Output is correct |
19 |
Correct |
123 ms |
39880 KB |
Output is correct |
20 |
Correct |
219 ms |
39876 KB |
Output is correct |
21 |
Correct |
16 ms |
29508 KB |
Output is correct |
22 |
Correct |
209 ms |
39876 KB |
Output is correct |
23 |
Correct |
269 ms |
39880 KB |
Output is correct |
24 |
Correct |
199 ms |
39880 KB |
Output is correct |
25 |
Correct |
286 ms |
39880 KB |
Output is correct |
26 |
Correct |
219 ms |
39876 KB |
Output is correct |
27 |
Correct |
233 ms |
39880 KB |
Output is correct |
28 |
Correct |
229 ms |
39880 KB |
Output is correct |
29 |
Correct |
179 ms |
39880 KB |
Output is correct |
30 |
Correct |
219 ms |
39876 KB |
Output is correct |
31 |
Correct |
486 ms |
35960 KB |
Output is correct |
32 |
Correct |
156 ms |
39876 KB |
Output is correct |
33 |
Correct |
486 ms |
38464 KB |
Output is correct |
34 |
Correct |
346 ms |
36916 KB |
Output is correct |
35 |
Correct |
316 ms |
38036 KB |
Output is correct |
36 |
Correct |
426 ms |
37660 KB |
Output is correct |
37 |
Correct |
399 ms |
37768 KB |
Output is correct |
38 |
Correct |
333 ms |
35660 KB |
Output is correct |
39 |
Correct |
176 ms |
39876 KB |
Output is correct |
40 |
Correct |
516 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 |
0 ms |
28716 KB |
Output is correct |
44 |
Correct |
0 ms |
28716 KB |
Output is correct |
45 |
Correct |
0 ms |
28716 KB |
Output is correct |
46 |
Correct |
0 ms |
28716 KB |
Output is correct |
47 |
Correct |
0 ms |
28716 KB |
Output is correct |
48 |
Correct |
3 ms |
28716 KB |
Output is correct |
49 |
Correct |
0 ms |
28716 KB |
Output is correct |
50 |
Correct |
3 ms |
28716 KB |
Output is correct |
51 |
Correct |
209 ms |
35696 KB |
Output is correct |
52 |
Correct |
266 ms |
39876 KB |
Output is correct |
53 |
Correct |
399 ms |
37752 KB |
Output is correct |
54 |
Correct |
249 ms |
36496 KB |
Output is correct |
55 |
Correct |
486 ms |
36416 KB |
Output is correct |
56 |
Correct |
213 ms |
39876 KB |
Output is correct |
57 |
Correct |
343 ms |
37632 KB |
Output is correct |
58 |
Correct |
389 ms |
36560 KB |
Output is correct |
59 |
Correct |
333 ms |
35664 KB |
Output is correct |
60 |
Correct |
219 ms |
39880 KB |
Output is correct |
61 |
Correct |
319 ms |
37688 KB |
Output is correct |
62 |
Correct |
426 ms |
37780 KB |
Output is correct |
63 |
Correct |
509 ms |
36588 KB |
Output is correct |
64 |
Correct |
253 ms |
39876 KB |
Output is correct |
65 |
Correct |
496 ms |
38760 KB |
Output is correct |
66 |
Correct |
269 ms |
36652 KB |
Output is correct |
67 |
Correct |
536 ms |
37332 KB |
Output is correct |
68 |
Correct |
243 ms |
39876 KB |
Output is correct |
69 |
Correct |
359 ms |
36796 KB |
Output is correct |
70 |
Correct |
393 ms |
37124 KB |
Output is correct |