/*
.:*+=%@@@@@@=-.
.:=@#@@@#@@#######%==*.
.-=####@######%*-.....:%##%.
.*@###########%+:--........-%@-
.*@##############@+--.........-:%-
.+##################@==%%%%=+*:----+.
.-@####################%++%@@@@@=+**%@@*
.%###################@%%@@@###@%+:--%@@%.
-@###################@%%%%*::*%++:-----=@+.
-#####################@%=++++++*:-------.-=:
.+####################@%++*::-:::--::*:::***=:
.@#####################%=++*::::-:::++*=##@@#@-
..#####################@%%=++**:::::**+%@#@%%##-..
.%####################@@%=+++*+****::*=@######@.
.=######################@%%==+==++**+=@%@##@###+:...
-#######################@@@%%%===++=@@@%=++===*::--...
-########################@@@@@@@%==%%=++==@@:::::*:--.
..:#########################@@@@@@%%======++++::-..:-.--...
%#############################@###@%%@@%==%=%*----.--.::---.
#############################################*-:*:-:---*---- .
#############################################*--*--:---*---:-.
#############################################+--::--::-*::-::.
###########################################+:*-.---.---.:---*-..
###########################################**:-----------------.
##########################################@::**:--::::::--:::::-
###########################################:--:*:::::::::**::*+*
###########################################=:::***::::::**:::*+*
############################@@@@@@#########@+****::::********+++
############################@%%%%%@@@@@@@###%+***::::::::***+==+
############################@%%%%%%%%%%%@####=+:::-::::-::*+=%%+
#############################@%%%%%%%%%%@#####=::--------:*=%@%+
%###########################@%%%%==%%%%%%@##@#=:------..-:+%@@%=
----------------------------------------------
--------------------------------------------
----------------------------------------------
--------------------------------------------
----------------------------------------------
o###########oo
o##" ""##o
o#" "##
o#" "#o
#" ## ## "##
#" ##
# ################### #
# #
# #
# #
# #
# #
# #
#o #
"#o ##
"#o ##
"#o o#"
"#o ##
"#o o#"
"#ooo ooo#######oo
############### "######o
o###"" "###o # ###
o###o oooo ### oo####"
o###**# #**# ############"
""##""""""""""########### #
# oooooooo#"#** ## #
# # # # ** ## #
#o# #o# *****###ooo#
##
## o###o
## o##***##
o########## #***#**##o
o##" ""### #***##***#
o#######o ### oo#### ##**####*#
o##" ""#############"" ##****###
##" ## ##*##*###
## ### ##### ##
## ### # ## #
## ## #
## ##
## ###
## ###oo
### ""###
###
###
*/
#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
//#pragma GCC target("avx,tune=native")
//float __attribute__((aligned(32)))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
int mysqrt(ll x){
int l = 0, r = 1e9 + 1;
while (r - l > 1){
int m = (l + r) / 2;
if (m * (ll)m <= x)
l = m;
else
r = m;
}
return l;
}
mt19937 rnd(1337);
ll AR = 19, BR = 13, CR = 23, XR = 228, YR = 322, MODR = 1e9 + 993;
ll myrand(){
ll ZR = (XR * AR + YR * BR + CR) % MODR;
XR = YR;
YR = ZR;
return ZR;
}
const int Mod = 1e9 + 7;
int bpow(int x, int y){
if (y == 0)
return 1;
if (y == 1)
return x;
int ret = bpow(x, y >> 1);
ret = (ret * (ll)ret) % Mod;
if (y & 1)
ret = (ret * (ll)x) % Mod;
return ret;
}
int bdiv(int x, int y){
return (x * (ll)bpow(y, Mod - 2)) % Mod;
}
void setmin(int &x, int y){
x = min(x, y);
}
void setmax(int &x, int y){
x = max(x, y);
}
int gcd(int a, int b){
return a ? gcd(b % a, a) : b;
}
const ll llinf = 2e18 + 100;
const int maxn = 1e5 + 100, maxw = (1 << 17) + 100, inf = 2e9 + 100, sq = 300, mod = 1e9 + 7, LG = 17;
int n;
vector<int> e[maxn];
int up[LG][maxn], q[LG][maxn];
int mas[maxn];
int dp[maxn];
int h[maxn];
int climb(int v, int hei){
for (int i = LG - 1; i >= 0; i--)
if (hei >= (1 << i))
hei -= (1 << i), v = up[i][v];
return v;
}
int getlca(int v, int u){
if (h[v] < h[u])
swap(v, u);
v = climb(v, h[v] - h[u]);
if (v == u)
return v;
for (int i = LG - 1; i >= 0; i--)
if (up[i][v] != up[i][u])
v = up[i][v], u = up[i][u];
return up[0][v];
}
void predfs(int v, int par){
for (auto i : e[v])
if (i != par)
h[i] = h[v] + 1, up[0][i] = v, predfs(i, v);
}
int get(int v, int it){
if (q[it][v] == -1){
if (it == 0)
q[it][v] = mas[v];
else
q[it][v] = get(v, it - 1) + get(up[it - 1][v], it - 1) - dp[climb(v, (1 << (it - 1)) - 1)];
}
return q[it][v];
}
int getpath(int v, int hei){
if (!hei)
return 0;
int r = get(v, 0);
hei--;
for (int i = LG - 1; i >= 0; i--)
if (hei >= (1 << i))
r += get(up[0][v], i) - dp[v], v = up[i][v], hei -= (1 << i);
return r;
}
void precalc(){
for (int i = 0; i < LG; i++)
for (int j = 0; j < n; j++)
q[i][j] = -1;
predfs(0, -1);
for (int i = 1; i < LG; i++)
for (int j = 0; j < n; j++)
up[i][j] = up[i - 1][up[i - 1][j]];
}
pair<pair<int, int>, int> que[maxn];
vector<int> qs[maxn];
void dfs(int v, int par){
for (auto i : e[v])
if (i != par){
dfs(i, v);
mas[v] += dp[i];
}
dp[v] = mas[v];
for (auto i : qs[v]){
int a = que[i].first.first, b = que[i].first.second, w = que[i].second;
int val = getpath(a, h[a] - h[v]) + getpath(b, h[b] - h[v]) + mas[v] + w;
if (a != v)
val -= dp[climb(a, h[a] - h[v] - 1)];
if (b != v)
val -= dp[climb(b, h[b] - h[v] - 1)];
dp[v] = max(dp[v], val);
}
}
int main()
{
#ifdef ONPC
//ifstream cin("a.in");
//ofstream cout("a.out");
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
#else
//ifstream cin("sprinklers.in");
//ofstream cout("sprinklers.out");
//freopen("road.in", "r", stdin);
//freopen("road.out", "w", stdout);
#endif // ONPC
//ios::sync_with_stdio(0);
//cin.tie(0);
//cout.tie(0);
scanf("%d", &n);
for (int i = 1; i < n; i++){
int v, u;
scanf("%d%d", &v, &u);
v--;
u--;
e[v].push_back(u);
e[u].push_back(v);
}
precalc();
int zap;
scanf("%d", &zap);
for (int i = 0; i < zap; i++){
int v, u, w;
scanf("%d%d%d", &v, &u, &w);
v--;
u--;
qs[getlca(v, u)].push_back(i);
que[i] = make_pair(make_pair(v, u), w);
}
dfs(0, -1);
printf("%d\n", dp[0]);
}
Compilation message
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:265:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
^
election_campaign.cpp:268:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &v, &u);
^
election_campaign.cpp:276:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &zap);
^
election_campaign.cpp:279:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &v, &u, &w);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
22360 KB |
Output is correct |
2 |
Correct |
0 ms |
22360 KB |
Output is correct |
3 |
Correct |
0 ms |
22360 KB |
Output is correct |
4 |
Correct |
0 ms |
22360 KB |
Output is correct |
5 |
Correct |
85 ms |
25660 KB |
Output is correct |
6 |
Correct |
52 ms |
34780 KB |
Output is correct |
7 |
Correct |
79 ms |
31536 KB |
Output is correct |
8 |
Correct |
54 ms |
26180 KB |
Output is correct |
9 |
Correct |
107 ms |
29680 KB |
Output is correct |
10 |
Correct |
43 ms |
26120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
22360 KB |
Output is correct |
2 |
Correct |
4 ms |
22360 KB |
Output is correct |
3 |
Correct |
4 ms |
22360 KB |
Output is correct |
4 |
Correct |
212 ms |
35836 KB |
Output is correct |
5 |
Correct |
216 ms |
35828 KB |
Output is correct |
6 |
Correct |
175 ms |
35836 KB |
Output is correct |
7 |
Correct |
222 ms |
35832 KB |
Output is correct |
8 |
Correct |
217 ms |
35836 KB |
Output is correct |
9 |
Correct |
188 ms |
35836 KB |
Output is correct |
10 |
Correct |
200 ms |
35832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
22360 KB |
Output is correct |
2 |
Correct |
4 ms |
22360 KB |
Output is correct |
3 |
Correct |
4 ms |
22360 KB |
Output is correct |
4 |
Correct |
212 ms |
35836 KB |
Output is correct |
5 |
Correct |
216 ms |
35828 KB |
Output is correct |
6 |
Correct |
175 ms |
35836 KB |
Output is correct |
7 |
Correct |
222 ms |
35832 KB |
Output is correct |
8 |
Correct |
217 ms |
35836 KB |
Output is correct |
9 |
Correct |
188 ms |
35836 KB |
Output is correct |
10 |
Correct |
200 ms |
35832 KB |
Output is correct |
11 |
Correct |
15 ms |
22624 KB |
Output is correct |
12 |
Correct |
204 ms |
35832 KB |
Output is correct |
13 |
Correct |
206 ms |
35832 KB |
Output is correct |
14 |
Correct |
198 ms |
35836 KB |
Output is correct |
15 |
Correct |
220 ms |
35832 KB |
Output is correct |
16 |
Correct |
176 ms |
35828 KB |
Output is correct |
17 |
Correct |
246 ms |
35836 KB |
Output is correct |
18 |
Correct |
216 ms |
35836 KB |
Output is correct |
19 |
Correct |
204 ms |
35832 KB |
Output is correct |
20 |
Correct |
227 ms |
35832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
174 ms |
26208 KB |
Output is correct |
2 |
Correct |
184 ms |
35836 KB |
Output is correct |
3 |
Correct |
479 ms |
32216 KB |
Output is correct |
4 |
Correct |
120 ms |
26836 KB |
Output is correct |
5 |
Correct |
393 ms |
31768 KB |
Output is correct |
6 |
Correct |
133 ms |
26960 KB |
Output is correct |
7 |
Correct |
512 ms |
31408 KB |
Output is correct |
8 |
Correct |
185 ms |
26608 KB |
Output is correct |
9 |
Correct |
172 ms |
35832 KB |
Output is correct |
10 |
Correct |
438 ms |
30360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
22360 KB |
Output is correct |
2 |
Correct |
0 ms |
22360 KB |
Output is correct |
3 |
Correct |
0 ms |
22360 KB |
Output is correct |
4 |
Correct |
0 ms |
22360 KB |
Output is correct |
5 |
Correct |
85 ms |
25660 KB |
Output is correct |
6 |
Correct |
52 ms |
34780 KB |
Output is correct |
7 |
Correct |
79 ms |
31536 KB |
Output is correct |
8 |
Correct |
54 ms |
26180 KB |
Output is correct |
9 |
Correct |
107 ms |
29680 KB |
Output is correct |
10 |
Correct |
43 ms |
26120 KB |
Output is correct |
11 |
Correct |
0 ms |
22360 KB |
Output is correct |
12 |
Correct |
4 ms |
22360 KB |
Output is correct |
13 |
Correct |
0 ms |
22360 KB |
Output is correct |
14 |
Correct |
3 ms |
22360 KB |
Output is correct |
15 |
Correct |
5 ms |
22360 KB |
Output is correct |
16 |
Correct |
0 ms |
22360 KB |
Output is correct |
17 |
Correct |
0 ms |
22360 KB |
Output is correct |
18 |
Correct |
5 ms |
22360 KB |
Output is correct |
19 |
Correct |
0 ms |
22360 KB |
Output is correct |
20 |
Correct |
6 ms |
22360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
22360 KB |
Output is correct |
2 |
Correct |
0 ms |
22360 KB |
Output is correct |
3 |
Correct |
0 ms |
22360 KB |
Output is correct |
4 |
Correct |
0 ms |
22360 KB |
Output is correct |
5 |
Correct |
85 ms |
25660 KB |
Output is correct |
6 |
Correct |
52 ms |
34780 KB |
Output is correct |
7 |
Correct |
79 ms |
31536 KB |
Output is correct |
8 |
Correct |
54 ms |
26180 KB |
Output is correct |
9 |
Correct |
107 ms |
29680 KB |
Output is correct |
10 |
Correct |
43 ms |
26120 KB |
Output is correct |
11 |
Correct |
0 ms |
22360 KB |
Output is correct |
12 |
Correct |
4 ms |
22360 KB |
Output is correct |
13 |
Correct |
4 ms |
22360 KB |
Output is correct |
14 |
Correct |
212 ms |
35836 KB |
Output is correct |
15 |
Correct |
216 ms |
35828 KB |
Output is correct |
16 |
Correct |
175 ms |
35836 KB |
Output is correct |
17 |
Correct |
222 ms |
35832 KB |
Output is correct |
18 |
Correct |
217 ms |
35836 KB |
Output is correct |
19 |
Correct |
188 ms |
35836 KB |
Output is correct |
20 |
Correct |
200 ms |
35832 KB |
Output is correct |
21 |
Correct |
15 ms |
22624 KB |
Output is correct |
22 |
Correct |
204 ms |
35832 KB |
Output is correct |
23 |
Correct |
206 ms |
35832 KB |
Output is correct |
24 |
Correct |
198 ms |
35836 KB |
Output is correct |
25 |
Correct |
220 ms |
35832 KB |
Output is correct |
26 |
Correct |
176 ms |
35828 KB |
Output is correct |
27 |
Correct |
246 ms |
35836 KB |
Output is correct |
28 |
Correct |
216 ms |
35836 KB |
Output is correct |
29 |
Correct |
204 ms |
35832 KB |
Output is correct |
30 |
Correct |
227 ms |
35832 KB |
Output is correct |
31 |
Correct |
174 ms |
26208 KB |
Output is correct |
32 |
Correct |
184 ms |
35836 KB |
Output is correct |
33 |
Correct |
479 ms |
32216 KB |
Output is correct |
34 |
Correct |
120 ms |
26836 KB |
Output is correct |
35 |
Correct |
393 ms |
31768 KB |
Output is correct |
36 |
Correct |
133 ms |
26960 KB |
Output is correct |
37 |
Correct |
512 ms |
31408 KB |
Output is correct |
38 |
Correct |
185 ms |
26608 KB |
Output is correct |
39 |
Correct |
172 ms |
35832 KB |
Output is correct |
40 |
Correct |
438 ms |
30360 KB |
Output is correct |
41 |
Correct |
0 ms |
22360 KB |
Output is correct |
42 |
Correct |
4 ms |
22360 KB |
Output is correct |
43 |
Correct |
0 ms |
22360 KB |
Output is correct |
44 |
Correct |
3 ms |
22360 KB |
Output is correct |
45 |
Correct |
5 ms |
22360 KB |
Output is correct |
46 |
Correct |
0 ms |
22360 KB |
Output is correct |
47 |
Correct |
0 ms |
22360 KB |
Output is correct |
48 |
Correct |
5 ms |
22360 KB |
Output is correct |
49 |
Correct |
0 ms |
22360 KB |
Output is correct |
50 |
Correct |
6 ms |
22360 KB |
Output is correct |
51 |
Correct |
186 ms |
26620 KB |
Output is correct |
52 |
Correct |
224 ms |
35832 KB |
Output is correct |
53 |
Correct |
472 ms |
30500 KB |
Output is correct |
54 |
Correct |
112 ms |
26824 KB |
Output is correct |
55 |
Correct |
207 ms |
26348 KB |
Output is correct |
56 |
Correct |
208 ms |
35828 KB |
Output is correct |
57 |
Correct |
387 ms |
31108 KB |
Output is correct |
58 |
Correct |
138 ms |
26724 KB |
Output is correct |
59 |
Correct |
180 ms |
26620 KB |
Output is correct |
60 |
Correct |
208 ms |
35832 KB |
Output is correct |
61 |
Correct |
410 ms |
31344 KB |
Output is correct |
62 |
Correct |
126 ms |
26920 KB |
Output is correct |
63 |
Correct |
183 ms |
26356 KB |
Output is correct |
64 |
Correct |
210 ms |
35832 KB |
Output is correct |
65 |
Correct |
478 ms |
31216 KB |
Output is correct |
66 |
Correct |
127 ms |
26804 KB |
Output is correct |
67 |
Correct |
190 ms |
26468 KB |
Output is correct |
68 |
Correct |
235 ms |
35832 KB |
Output is correct |
69 |
Correct |
409 ms |
29832 KB |
Output is correct |
70 |
Correct |
135 ms |
26784 KB |
Output is correct |