#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+10, LOG = 20;
vector<array<int, 3>> paths[N];
ll in[N], out[N], d[N], L[N][LOG], t[2*N][2], sum[N], dp[N];
ll n, m;
vector<int> edges[N];
void dfs(int x, int p) {
static int timer = 0;
in[x] = ++timer;
L[x][0] = p;
d[x] = d[p] + 1;
for(int i = 1; i < LOG; ++i) {
L[x][i] = L[L[x][i - 1]][i - 1];
}
for(int to : edges[x]) {
if(to != p) {
dfs(to, x);
}
}
out[x] = ++timer;
}
bool is_anc(int x, int y) {
return !x || in[x] <= in[y] && out[x] >= out[y];
}
int lca(int x, int y) {
if(is_anc(x, y)) return x;
if(is_anc(y, x)) return y;
for(int i = LOG-1; ~i; --i) {
if(!is_anc(L[x][i], y)) {
x = L[x][i];
}
}
return L[x][0];
}
int dist(int x, int y) {
return abs(d[x] - d[y]);
}
int find_anc(int x, int d) {
for(int bit = 0; bit < LOG; ++bit) {
if(d >> bit & 1) x = L[x][bit];
}
return x;
}
void maxi(ll &x, ll y) {
if(x < y) x = y;
}
void update(ll x, ll v, ll id) {
for(; x <= 2*n; x += x & -x) {
t[x][id] += v;
}
}
ll query(ll x, ll id) {
ll sum = 0;
for(; x > 0; x -= x & -x) {
sum += t[x][id];
}
return sum;
}
ll query(ll l, ll r, ll id) {
return query(r, id) - query(l-1, id);
}
void solve(int x, int p) {
for(int to : edges[x]) {
if(to != p) {
solve(to, x);
sum[x] += dp[to];
}
}
dp[x] = sum[x];
for(auto &q : paths[x]) {
int a = q[0];
int b = q[1];
int c = q[2];
ll sum0 = query(in[x], in[a], 0);
ll sum1 = query(in[x], in[b], 0);
ll s = sum0 + sum1 + sum[x];
if(a != x) {
int pa = find_anc(a, dist(x, a) - 1);
s -= query(in[pa], in[a], 1);
}
if(b != x) {
int pb = find_anc(b, dist(x, b) - 1);
s -= query(in[pb], in[b], 1);
}
maxi(dp[x], s + c);
}
update(in[x], sum[x], 0);
update(out[x], -sum[x], 0);
update(in[x], dp[x], 1);
update(out[x], -dp[x], 1);
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
for(int i = 0; i < n-1; ++i) {
int u, v; cin >> u >> v;
edges[u].push_back(v);
edges[v].push_back(u);
}
dfs(1, 0);
cin >> m;
for(int i = 0; i < m; ++i) {
int u, v, c; cin >> u >> v >> c;
paths[lca(u, v)].push_back({u, v, c});
}
solve(1, 0);
cout << dp[1] << endl;
}
Compilation message
election_campaign.cpp: In function 'bool is_anc(int, int)':
election_campaign.cpp:25:30: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
25 | return !x || in[x] <= in[y] && out[x] >= out[y];
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
5324 KB |
Output is correct |
5 |
Correct |
117 ms |
32068 KB |
Output is correct |
6 |
Correct |
71 ms |
44492 KB |
Output is correct |
7 |
Correct |
129 ms |
40132 KB |
Output is correct |
8 |
Correct |
99 ms |
31864 KB |
Output is correct |
9 |
Correct |
127 ms |
37656 KB |
Output is correct |
10 |
Correct |
108 ms |
31756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
5012 KB |
Output is correct |
3 |
Correct |
4 ms |
5452 KB |
Output is correct |
4 |
Correct |
149 ms |
47732 KB |
Output is correct |
5 |
Correct |
161 ms |
47812 KB |
Output is correct |
6 |
Correct |
135 ms |
47776 KB |
Output is correct |
7 |
Correct |
163 ms |
47812 KB |
Output is correct |
8 |
Correct |
167 ms |
47760 KB |
Output is correct |
9 |
Correct |
152 ms |
47780 KB |
Output is correct |
10 |
Correct |
168 ms |
47708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
5012 KB |
Output is correct |
3 |
Correct |
4 ms |
5452 KB |
Output is correct |
4 |
Correct |
149 ms |
47732 KB |
Output is correct |
5 |
Correct |
161 ms |
47812 KB |
Output is correct |
6 |
Correct |
135 ms |
47776 KB |
Output is correct |
7 |
Correct |
163 ms |
47812 KB |
Output is correct |
8 |
Correct |
167 ms |
47760 KB |
Output is correct |
9 |
Correct |
152 ms |
47780 KB |
Output is correct |
10 |
Correct |
168 ms |
47708 KB |
Output is correct |
11 |
Correct |
15 ms |
6220 KB |
Output is correct |
12 |
Correct |
156 ms |
48004 KB |
Output is correct |
13 |
Correct |
180 ms |
48024 KB |
Output is correct |
14 |
Correct |
138 ms |
48056 KB |
Output is correct |
15 |
Correct |
196 ms |
47996 KB |
Output is correct |
16 |
Correct |
148 ms |
48068 KB |
Output is correct |
17 |
Correct |
158 ms |
48020 KB |
Output is correct |
18 |
Correct |
161 ms |
48008 KB |
Output is correct |
19 |
Correct |
161 ms |
48040 KB |
Output is correct |
20 |
Correct |
188 ms |
48004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
247 ms |
34832 KB |
Output is correct |
2 |
Correct |
166 ms |
47812 KB |
Output is correct |
3 |
Correct |
324 ms |
42996 KB |
Output is correct |
4 |
Correct |
183 ms |
34604 KB |
Output is correct |
5 |
Correct |
276 ms |
42140 KB |
Output is correct |
6 |
Correct |
192 ms |
34556 KB |
Output is correct |
7 |
Correct |
311 ms |
41748 KB |
Output is correct |
8 |
Correct |
218 ms |
35124 KB |
Output is correct |
9 |
Correct |
150 ms |
47944 KB |
Output is correct |
10 |
Correct |
314 ms |
40308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
5324 KB |
Output is correct |
5 |
Correct |
117 ms |
32068 KB |
Output is correct |
6 |
Correct |
71 ms |
44492 KB |
Output is correct |
7 |
Correct |
129 ms |
40132 KB |
Output is correct |
8 |
Correct |
99 ms |
31864 KB |
Output is correct |
9 |
Correct |
127 ms |
37656 KB |
Output is correct |
10 |
Correct |
108 ms |
31756 KB |
Output is correct |
11 |
Correct |
5 ms |
5328 KB |
Output is correct |
12 |
Correct |
5 ms |
5452 KB |
Output is correct |
13 |
Correct |
5 ms |
5324 KB |
Output is correct |
14 |
Correct |
5 ms |
5324 KB |
Output is correct |
15 |
Correct |
5 ms |
5292 KB |
Output is correct |
16 |
Correct |
5 ms |
5312 KB |
Output is correct |
17 |
Correct |
6 ms |
5288 KB |
Output is correct |
18 |
Correct |
5 ms |
5324 KB |
Output is correct |
19 |
Correct |
5 ms |
5324 KB |
Output is correct |
20 |
Correct |
5 ms |
5452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
5324 KB |
Output is correct |
5 |
Correct |
117 ms |
32068 KB |
Output is correct |
6 |
Correct |
71 ms |
44492 KB |
Output is correct |
7 |
Correct |
129 ms |
40132 KB |
Output is correct |
8 |
Correct |
99 ms |
31864 KB |
Output is correct |
9 |
Correct |
127 ms |
37656 KB |
Output is correct |
10 |
Correct |
108 ms |
31756 KB |
Output is correct |
11 |
Correct |
4 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
5012 KB |
Output is correct |
13 |
Correct |
4 ms |
5452 KB |
Output is correct |
14 |
Correct |
149 ms |
47732 KB |
Output is correct |
15 |
Correct |
161 ms |
47812 KB |
Output is correct |
16 |
Correct |
135 ms |
47776 KB |
Output is correct |
17 |
Correct |
163 ms |
47812 KB |
Output is correct |
18 |
Correct |
167 ms |
47760 KB |
Output is correct |
19 |
Correct |
152 ms |
47780 KB |
Output is correct |
20 |
Correct |
168 ms |
47708 KB |
Output is correct |
21 |
Correct |
15 ms |
6220 KB |
Output is correct |
22 |
Correct |
156 ms |
48004 KB |
Output is correct |
23 |
Correct |
180 ms |
48024 KB |
Output is correct |
24 |
Correct |
138 ms |
48056 KB |
Output is correct |
25 |
Correct |
196 ms |
47996 KB |
Output is correct |
26 |
Correct |
148 ms |
48068 KB |
Output is correct |
27 |
Correct |
158 ms |
48020 KB |
Output is correct |
28 |
Correct |
161 ms |
48008 KB |
Output is correct |
29 |
Correct |
161 ms |
48040 KB |
Output is correct |
30 |
Correct |
188 ms |
48004 KB |
Output is correct |
31 |
Correct |
247 ms |
34832 KB |
Output is correct |
32 |
Correct |
166 ms |
47812 KB |
Output is correct |
33 |
Correct |
324 ms |
42996 KB |
Output is correct |
34 |
Correct |
183 ms |
34604 KB |
Output is correct |
35 |
Correct |
276 ms |
42140 KB |
Output is correct |
36 |
Correct |
192 ms |
34556 KB |
Output is correct |
37 |
Correct |
311 ms |
41748 KB |
Output is correct |
38 |
Correct |
218 ms |
35124 KB |
Output is correct |
39 |
Correct |
150 ms |
47944 KB |
Output is correct |
40 |
Correct |
314 ms |
40308 KB |
Output is correct |
41 |
Correct |
5 ms |
5328 KB |
Output is correct |
42 |
Correct |
5 ms |
5452 KB |
Output is correct |
43 |
Correct |
5 ms |
5324 KB |
Output is correct |
44 |
Correct |
5 ms |
5324 KB |
Output is correct |
45 |
Correct |
5 ms |
5292 KB |
Output is correct |
46 |
Correct |
5 ms |
5312 KB |
Output is correct |
47 |
Correct |
6 ms |
5288 KB |
Output is correct |
48 |
Correct |
5 ms |
5324 KB |
Output is correct |
49 |
Correct |
5 ms |
5324 KB |
Output is correct |
50 |
Correct |
5 ms |
5452 KB |
Output is correct |
51 |
Correct |
237 ms |
35388 KB |
Output is correct |
52 |
Correct |
176 ms |
47996 KB |
Output is correct |
53 |
Correct |
359 ms |
40736 KB |
Output is correct |
54 |
Correct |
168 ms |
34836 KB |
Output is correct |
55 |
Correct |
252 ms |
35276 KB |
Output is correct |
56 |
Correct |
160 ms |
48012 KB |
Output is correct |
57 |
Correct |
273 ms |
41680 KB |
Output is correct |
58 |
Correct |
178 ms |
34776 KB |
Output is correct |
59 |
Correct |
222 ms |
35484 KB |
Output is correct |
60 |
Correct |
162 ms |
47984 KB |
Output is correct |
61 |
Correct |
275 ms |
41760 KB |
Output is correct |
62 |
Correct |
185 ms |
34564 KB |
Output is correct |
63 |
Correct |
237 ms |
35080 KB |
Output is correct |
64 |
Correct |
161 ms |
48060 KB |
Output is correct |
65 |
Correct |
330 ms |
41708 KB |
Output is correct |
66 |
Correct |
163 ms |
34908 KB |
Output is correct |
67 |
Correct |
231 ms |
35156 KB |
Output is correct |
68 |
Correct |
153 ms |
47996 KB |
Output is correct |
69 |
Correct |
249 ms |
39820 KB |
Output is correct |
70 |
Correct |
175 ms |
34920 KB |
Output is correct |