#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define file "input"
void setIO() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(file".inp", "r")) {
freopen(file".inp", "r", stdin);
freopen(file".out", "w", stdout);
}
}
const int N = 1e5+5, LOG = 18;
int n, m, anc[N][LOG], st[N], en[N], cnt;
ll bit[N], sum[N], best[N];
vector<pair<ii, int>> path[N];
vector<int> graph[N];
void modify(int i, int v) {
for(; i<=n; i+=i&-i) bit[i] += v;
}
ll query(int i) {
ll ans = 0;
for(; i>=1; i-=i&-i) ans += bit[i];
return ans;
}
void prep(int u, int p) {
anc[u][0] = p;
foru(j, 1, LOG-1) anc[u][j] = anc[anc[u][j-1]][j-1];
st[u] = ++cnt;
fore(v, graph[u]) if (v != p) prep(v, u);
en[u] = cnt;
}
bool isAnc(int u, int v) {
return st[u] <= st[v] && en[v] <= en[u];
}
int lca(int u, int v) {
if (isAnc(u, v)) return u;
ford(j, LOG-1, 0) {
if (!isAnc(anc[u][j], v)) {
u = anc[u][j];
}
}
return anc[u][0];
}
void dfs(int u, int p) {
fore(v, graph[u]) if (v != p) {
dfs(v, u);
sum[u] += best[v];
}
best[u] = sum[u];
fore(plan, path[u]) {
int x = plan.first.first, y = plan.first.second, rew = plan.second;
ll tmp = rew;
if (x != u) tmp += query(st[x]);
if (y != u) tmp += query(st[y]);
best[u] = max(best[u], tmp + sum[u]);
}
// cout << u << " " << sum[u] << " " << best[u] << "\n";
modify(st[u], sum[u] - best[u]);
modify(en[u]+1, best[u] - sum[u]);
}
int main() {
setIO();
cin >> n;
foru(i, 2, n) {
int u, v;
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
en[0] = n+1;
prep(1, 0);
cin >> m;
foru(i, 1, m) {
int u, v, w;
cin >> u >> v >> w;
path[lca(u, v)].push_back({{u, v}, w});
}
dfs(1, 0);
cout << best[1];
}
Compilation message
election_campaign.cpp: In function 'void setIO()':
election_campaign.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen(file".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:20:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | freopen(file".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Correct |
1 ms |
8744 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
42 ms |
19540 KB |
Output is correct |
6 |
Correct |
32 ms |
27480 KB |
Output is correct |
7 |
Correct |
47 ms |
24656 KB |
Output is correct |
8 |
Correct |
37 ms |
20132 KB |
Output is correct |
9 |
Correct |
46 ms |
23124 KB |
Output is correct |
10 |
Correct |
34 ms |
20312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
72 ms |
30632 KB |
Output is correct |
5 |
Correct |
73 ms |
30548 KB |
Output is correct |
6 |
Correct |
64 ms |
30764 KB |
Output is correct |
7 |
Correct |
79 ms |
30536 KB |
Output is correct |
8 |
Correct |
75 ms |
30740 KB |
Output is correct |
9 |
Correct |
65 ms |
30516 KB |
Output is correct |
10 |
Correct |
74 ms |
30548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
72 ms |
30632 KB |
Output is correct |
5 |
Correct |
73 ms |
30548 KB |
Output is correct |
6 |
Correct |
64 ms |
30764 KB |
Output is correct |
7 |
Correct |
79 ms |
30536 KB |
Output is correct |
8 |
Correct |
75 ms |
30740 KB |
Output is correct |
9 |
Correct |
65 ms |
30516 KB |
Output is correct |
10 |
Correct |
74 ms |
30548 KB |
Output is correct |
11 |
Correct |
8 ms |
9564 KB |
Output is correct |
12 |
Correct |
78 ms |
30920 KB |
Output is correct |
13 |
Correct |
76 ms |
30984 KB |
Output is correct |
14 |
Correct |
71 ms |
31116 KB |
Output is correct |
15 |
Correct |
74 ms |
30804 KB |
Output is correct |
16 |
Correct |
64 ms |
30800 KB |
Output is correct |
17 |
Correct |
76 ms |
30804 KB |
Output is correct |
18 |
Correct |
75 ms |
30988 KB |
Output is correct |
19 |
Correct |
73 ms |
30964 KB |
Output is correct |
20 |
Correct |
75 ms |
30804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
22604 KB |
Output is correct |
2 |
Correct |
65 ms |
30556 KB |
Output is correct |
3 |
Correct |
101 ms |
27472 KB |
Output is correct |
4 |
Correct |
69 ms |
22988 KB |
Output is correct |
5 |
Correct |
92 ms |
27068 KB |
Output is correct |
6 |
Correct |
65 ms |
22960 KB |
Output is correct |
7 |
Correct |
98 ms |
26768 KB |
Output is correct |
8 |
Correct |
97 ms |
22864 KB |
Output is correct |
9 |
Correct |
64 ms |
30564 KB |
Output is correct |
10 |
Correct |
109 ms |
25644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Correct |
1 ms |
8744 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
42 ms |
19540 KB |
Output is correct |
6 |
Correct |
32 ms |
27480 KB |
Output is correct |
7 |
Correct |
47 ms |
24656 KB |
Output is correct |
8 |
Correct |
37 ms |
20132 KB |
Output is correct |
9 |
Correct |
46 ms |
23124 KB |
Output is correct |
10 |
Correct |
34 ms |
20312 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
3 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8792 KB |
Output is correct |
18 |
Correct |
2 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Correct |
1 ms |
8744 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
42 ms |
19540 KB |
Output is correct |
6 |
Correct |
32 ms |
27480 KB |
Output is correct |
7 |
Correct |
47 ms |
24656 KB |
Output is correct |
8 |
Correct |
37 ms |
20132 KB |
Output is correct |
9 |
Correct |
46 ms |
23124 KB |
Output is correct |
10 |
Correct |
34 ms |
20312 KB |
Output is correct |
11 |
Correct |
2 ms |
8792 KB |
Output is correct |
12 |
Correct |
2 ms |
8792 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
72 ms |
30632 KB |
Output is correct |
15 |
Correct |
73 ms |
30548 KB |
Output is correct |
16 |
Correct |
64 ms |
30764 KB |
Output is correct |
17 |
Correct |
79 ms |
30536 KB |
Output is correct |
18 |
Correct |
75 ms |
30740 KB |
Output is correct |
19 |
Correct |
65 ms |
30516 KB |
Output is correct |
20 |
Correct |
74 ms |
30548 KB |
Output is correct |
21 |
Correct |
8 ms |
9564 KB |
Output is correct |
22 |
Correct |
78 ms |
30920 KB |
Output is correct |
23 |
Correct |
76 ms |
30984 KB |
Output is correct |
24 |
Correct |
71 ms |
31116 KB |
Output is correct |
25 |
Correct |
74 ms |
30804 KB |
Output is correct |
26 |
Correct |
64 ms |
30800 KB |
Output is correct |
27 |
Correct |
76 ms |
30804 KB |
Output is correct |
28 |
Correct |
75 ms |
30988 KB |
Output is correct |
29 |
Correct |
73 ms |
30964 KB |
Output is correct |
30 |
Correct |
75 ms |
30804 KB |
Output is correct |
31 |
Correct |
85 ms |
22604 KB |
Output is correct |
32 |
Correct |
65 ms |
30556 KB |
Output is correct |
33 |
Correct |
101 ms |
27472 KB |
Output is correct |
34 |
Correct |
69 ms |
22988 KB |
Output is correct |
35 |
Correct |
92 ms |
27068 KB |
Output is correct |
36 |
Correct |
65 ms |
22960 KB |
Output is correct |
37 |
Correct |
98 ms |
26768 KB |
Output is correct |
38 |
Correct |
97 ms |
22864 KB |
Output is correct |
39 |
Correct |
64 ms |
30564 KB |
Output is correct |
40 |
Correct |
109 ms |
25644 KB |
Output is correct |
41 |
Correct |
2 ms |
8796 KB |
Output is correct |
42 |
Correct |
2 ms |
8796 KB |
Output is correct |
43 |
Correct |
2 ms |
8796 KB |
Output is correct |
44 |
Correct |
2 ms |
8796 KB |
Output is correct |
45 |
Correct |
2 ms |
8796 KB |
Output is correct |
46 |
Correct |
3 ms |
8796 KB |
Output is correct |
47 |
Correct |
2 ms |
8792 KB |
Output is correct |
48 |
Correct |
2 ms |
8796 KB |
Output is correct |
49 |
Correct |
2 ms |
8796 KB |
Output is correct |
50 |
Correct |
2 ms |
8796 KB |
Output is correct |
51 |
Correct |
92 ms |
23180 KB |
Output is correct |
52 |
Correct |
74 ms |
30800 KB |
Output is correct |
53 |
Correct |
149 ms |
26120 KB |
Output is correct |
54 |
Correct |
66 ms |
23180 KB |
Output is correct |
55 |
Correct |
80 ms |
22888 KB |
Output is correct |
56 |
Correct |
75 ms |
30800 KB |
Output is correct |
57 |
Correct |
88 ms |
26984 KB |
Output is correct |
58 |
Correct |
69 ms |
23048 KB |
Output is correct |
59 |
Correct |
109 ms |
22868 KB |
Output is correct |
60 |
Correct |
79 ms |
30808 KB |
Output is correct |
61 |
Correct |
102 ms |
27068 KB |
Output is correct |
62 |
Correct |
76 ms |
23216 KB |
Output is correct |
63 |
Correct |
84 ms |
22692 KB |
Output is correct |
64 |
Correct |
80 ms |
30976 KB |
Output is correct |
65 |
Correct |
101 ms |
26916 KB |
Output is correct |
66 |
Correct |
68 ms |
23184 KB |
Output is correct |
67 |
Correct |
81 ms |
22984 KB |
Output is correct |
68 |
Correct |
74 ms |
30832 KB |
Output is correct |
69 |
Correct |
84 ms |
25680 KB |
Output is correct |
70 |
Correct |
73 ms |
23380 KB |
Output is correct |