This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
.:*+=%@@@@@@=-.
.:=@#@@@#@@#######%==*.
.-=####@######%*-.....:%##%.
.*@###########%+:--........-%@-
.*@##############@+--.........-:%-
.+##################@==%%%%=+*:----+.
.-@####################%++%@@@@@=+**%@@*
.%###################@%%@@@###@%+:--%@@%.
-@###################@%%%%*::*%++:-----=@+.
-#####################@%=++++++*:-------.-=:
.+####################@%++*::-:::--::*:::***=:
.@#####################%=++*::::-:::++*=##@@#@-
..#####################@%%=++**:::::**+%@#@%%##-..
.%####################@@%=+++*+****::*=@######@.
.=######################@%%==+==++**+=@%@##@###+:...
-#######################@@@%%%===++=@@@%=++===*::--...
-########################@@@@@@@%==%%=++==@@:::::*:--.
..:#########################@@@@@@%%======++++::-..:-.--...
%#############################@###@%%@@%==%=%*----.--.::---.
#############################################*-:*:-:---*---- .
#############################################*--*--:---*---:-.
#############################################+--::--::-*::-::.
###########################################+:*-.---.---.:---*-..
###########################################**:-----------------.
##########################################@::**:--::::::--:::::-
###########################################:--:*:::::::::**::*+*
###########################################=:::***::::::**:::*+*
############################@@@@@@#########@+****::::********+++
############################@%%%%%@@@@@@@###%+***::::::::***+==+
############################@%%%%%%%%%%%@####=+:::-::::-::*+=%%+
#############################@%%%%%%%%%%@#####=::--------:*=%@%+
%###########################@%%%%==%%%%%%@##@#=:------..-:+%@@%=
----------------------------------------------
--------------------------------------------
----------------------------------------------
--------------------------------------------
----------------------------------------------
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 pep[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[pep[it - 1][v]];
}
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]];
for (int j = 0; j < n; j++)
pep[0][j] = j;
for (int i = 1; i < LG; i++)
for (int j = 0; j < n; j++)
pep[i][j] = pep[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 (stderr)
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:272:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
^
election_campaign.cpp:275: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:283:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &zap);
^
election_campaign.cpp:286: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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |