#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REPO(i,n) for(int (i)=1; (i)<=(int)(n); (i)++)
#define SZ(v) ((int)(v).size())
#define ALL(v) (v).begin(),(v).end()
#define one first
#define two second
typedef long long ll;
typedef pair<int, int> pi;
const int INF = 0x3f2f1f0f;
const ll LINF = 1ll * INF * INF;
const int MAX_N = 1e5 + 10, LOG_N = 20;
int N, Q, P[LOG_N][MAX_N], D[MAX_N], St[MAX_N], En[MAX_N], Ix;
vector<int> Ed[MAX_N];
int getLCA(int a, int b) {
if(D[a] < D[b]) swap(a, b);
for(int p=0; p<LOG_N; p++) if((1<<p) & (D[a]-D[b])) a = P[p][a];
if(a == b) return a;
for(int p=LOG_N-1; p>=0; p--)
if(P[p][a] != P[p][b])
a = P[p][a], b = P[p][b];
a = P[0][a];
return a;
}
void dfs(int v, int p) {
St[v] = Ix++; P[0][v] = p; D[v] = D[p] + 1;
for(int w : Ed[v]) if(w != p) dfs(w, v);
En[v] = Ix-1;
}
struct IDX {
int P; vector<int> val;
IDX() {}
IDX(int n) {
for(P=1; P<n; P<<=1);
val = vector<int>(2*P, 0);
}
void update(int a, int b, int v) {
a += P; b += P;
while(a<=b) {
if(a%2==1) val[a++] += v;
if(b%2==0) val[b--] += v;
a >>= 1; b >>= 1;
}
}
int getAll(int v) {
int res = val[v += P];
while(v >>= 1) res += val[v];
return res;
}
}idx;
typedef tuple<int, int, int> ti;
vector<ti> Qs[MAX_N];
int getAns(int v, int p) {
int notUse = 0;
for(int w : Ed[v]) if(w != p) {
notUse += getAns(w, v);
}
int use = notUse;
for(ti &e : Qs[v]) {
int x, y, c; tie(x, y, c) = e;
use = max(use, notUse + idx.getAll(St[x]) + idx.getAll(St[y]) + c);
}
idx.update(St[v], En[v], notUse - use);
// printf("%d : %d %d\n", v, notUse, use);
return use;
}
int main() {
cin >> N;
REP(i, N-1) {
int x, y; scanf("%d%d", &x, &y);
Ed[x].push_back(y); Ed[y].push_back(x);
}
dfs(1, 0);
idx = IDX(Ix);
for(int p=1; p<LOG_N; p++) for(int i=1; i<=N; i++) P[p][i] = P[p-1][P[p-1][i]];
cin >> Q;
while(Q--) {
int x, y, c; scanf("%d%d%d", &x, &y, &c);
// printf("[%d %d %d]\n", x, y, c);
int l = getLCA(x, y);
Qs[l].push_back(ti(x, y, c));
}
printf("%d\n", getAns(1, 0));
return 0;
}
Compilation message
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:78:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x, y; scanf("%d%d", &x, &y);
^
election_campaign.cpp:86:43: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x, y, c; scanf("%d%d%d", &x, &y, &c);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
15696 KB |
Output is correct |
2 |
Correct |
3 ms |
15696 KB |
Output is correct |
3 |
Correct |
3 ms |
15696 KB |
Output is correct |
4 |
Correct |
6 ms |
15696 KB |
Output is correct |
5 |
Correct |
133 ms |
20024 KB |
Output is correct |
6 |
Correct |
63 ms |
26020 KB |
Output is correct |
7 |
Correct |
99 ms |
23860 KB |
Output is correct |
8 |
Correct |
96 ms |
20544 KB |
Output is correct |
9 |
Correct |
113 ms |
22620 KB |
Output is correct |
10 |
Correct |
93 ms |
20484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
15696 KB |
Output is correct |
2 |
Correct |
3 ms |
15696 KB |
Output is correct |
3 |
Correct |
0 ms |
15696 KB |
Output is correct |
4 |
Correct |
206 ms |
28000 KB |
Output is correct |
5 |
Correct |
229 ms |
27996 KB |
Output is correct |
6 |
Correct |
186 ms |
27996 KB |
Output is correct |
7 |
Correct |
246 ms |
27996 KB |
Output is correct |
8 |
Correct |
139 ms |
27992 KB |
Output is correct |
9 |
Correct |
183 ms |
28000 KB |
Output is correct |
10 |
Correct |
223 ms |
27992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
15696 KB |
Output is correct |
2 |
Correct |
3 ms |
15696 KB |
Output is correct |
3 |
Correct |
0 ms |
15696 KB |
Output is correct |
4 |
Correct |
206 ms |
28000 KB |
Output is correct |
5 |
Correct |
229 ms |
27996 KB |
Output is correct |
6 |
Correct |
186 ms |
27996 KB |
Output is correct |
7 |
Correct |
246 ms |
27996 KB |
Output is correct |
8 |
Correct |
139 ms |
27992 KB |
Output is correct |
9 |
Correct |
183 ms |
28000 KB |
Output is correct |
10 |
Correct |
223 ms |
27992 KB |
Output is correct |
11 |
Correct |
13 ms |
16224 KB |
Output is correct |
12 |
Correct |
196 ms |
27996 KB |
Output is correct |
13 |
Correct |
193 ms |
27992 KB |
Output is correct |
14 |
Correct |
166 ms |
27996 KB |
Output is correct |
15 |
Correct |
229 ms |
28000 KB |
Output is correct |
16 |
Correct |
173 ms |
28000 KB |
Output is correct |
17 |
Correct |
229 ms |
28000 KB |
Output is correct |
18 |
Correct |
136 ms |
27992 KB |
Output is correct |
19 |
Correct |
176 ms |
27996 KB |
Output is correct |
20 |
Correct |
219 ms |
28000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
269 ms |
21716 KB |
Output is correct |
2 |
Correct |
166 ms |
27996 KB |
Output is correct |
3 |
Correct |
349 ms |
25576 KB |
Output is correct |
4 |
Correct |
139 ms |
22484 KB |
Output is correct |
5 |
Correct |
279 ms |
25232 KB |
Output is correct |
6 |
Correct |
173 ms |
22820 KB |
Output is correct |
7 |
Correct |
296 ms |
24884 KB |
Output is correct |
8 |
Correct |
166 ms |
21740 KB |
Output is correct |
9 |
Correct |
179 ms |
27996 KB |
Output is correct |
10 |
Correct |
346 ms |
24336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
15696 KB |
Output is correct |
2 |
Correct |
3 ms |
15696 KB |
Output is correct |
3 |
Correct |
3 ms |
15696 KB |
Output is correct |
4 |
Correct |
6 ms |
15696 KB |
Output is correct |
5 |
Correct |
133 ms |
20024 KB |
Output is correct |
6 |
Correct |
63 ms |
26020 KB |
Output is correct |
7 |
Correct |
99 ms |
23860 KB |
Output is correct |
8 |
Correct |
96 ms |
20544 KB |
Output is correct |
9 |
Correct |
113 ms |
22620 KB |
Output is correct |
10 |
Correct |
93 ms |
20484 KB |
Output is correct |
11 |
Correct |
0 ms |
15828 KB |
Output is correct |
12 |
Correct |
3 ms |
15828 KB |
Output is correct |
13 |
Correct |
0 ms |
15828 KB |
Output is correct |
14 |
Correct |
3 ms |
15828 KB |
Output is correct |
15 |
Correct |
3 ms |
15828 KB |
Output is correct |
16 |
Correct |
0 ms |
15828 KB |
Output is correct |
17 |
Correct |
0 ms |
15828 KB |
Output is correct |
18 |
Correct |
0 ms |
15828 KB |
Output is correct |
19 |
Correct |
0 ms |
15828 KB |
Output is correct |
20 |
Correct |
0 ms |
15828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
15696 KB |
Output is correct |
2 |
Correct |
3 ms |
15696 KB |
Output is correct |
3 |
Correct |
3 ms |
15696 KB |
Output is correct |
4 |
Correct |
6 ms |
15696 KB |
Output is correct |
5 |
Correct |
133 ms |
20024 KB |
Output is correct |
6 |
Correct |
63 ms |
26020 KB |
Output is correct |
7 |
Correct |
99 ms |
23860 KB |
Output is correct |
8 |
Correct |
96 ms |
20544 KB |
Output is correct |
9 |
Correct |
113 ms |
22620 KB |
Output is correct |
10 |
Correct |
93 ms |
20484 KB |
Output is correct |
11 |
Correct |
0 ms |
15696 KB |
Output is correct |
12 |
Correct |
3 ms |
15696 KB |
Output is correct |
13 |
Correct |
0 ms |
15696 KB |
Output is correct |
14 |
Correct |
206 ms |
28000 KB |
Output is correct |
15 |
Correct |
229 ms |
27996 KB |
Output is correct |
16 |
Correct |
186 ms |
27996 KB |
Output is correct |
17 |
Correct |
246 ms |
27996 KB |
Output is correct |
18 |
Correct |
139 ms |
27992 KB |
Output is correct |
19 |
Correct |
183 ms |
28000 KB |
Output is correct |
20 |
Correct |
223 ms |
27992 KB |
Output is correct |
21 |
Correct |
13 ms |
16224 KB |
Output is correct |
22 |
Correct |
196 ms |
27996 KB |
Output is correct |
23 |
Correct |
193 ms |
27992 KB |
Output is correct |
24 |
Correct |
166 ms |
27996 KB |
Output is correct |
25 |
Correct |
229 ms |
28000 KB |
Output is correct |
26 |
Correct |
173 ms |
28000 KB |
Output is correct |
27 |
Correct |
229 ms |
28000 KB |
Output is correct |
28 |
Correct |
136 ms |
27992 KB |
Output is correct |
29 |
Correct |
176 ms |
27996 KB |
Output is correct |
30 |
Correct |
219 ms |
28000 KB |
Output is correct |
31 |
Correct |
269 ms |
21716 KB |
Output is correct |
32 |
Correct |
166 ms |
27996 KB |
Output is correct |
33 |
Correct |
349 ms |
25576 KB |
Output is correct |
34 |
Correct |
139 ms |
22484 KB |
Output is correct |
35 |
Correct |
279 ms |
25232 KB |
Output is correct |
36 |
Correct |
173 ms |
22820 KB |
Output is correct |
37 |
Correct |
296 ms |
24884 KB |
Output is correct |
38 |
Correct |
166 ms |
21740 KB |
Output is correct |
39 |
Correct |
179 ms |
27996 KB |
Output is correct |
40 |
Correct |
346 ms |
24336 KB |
Output is correct |
41 |
Correct |
0 ms |
15828 KB |
Output is correct |
42 |
Correct |
3 ms |
15828 KB |
Output is correct |
43 |
Correct |
0 ms |
15828 KB |
Output is correct |
44 |
Correct |
3 ms |
15828 KB |
Output is correct |
45 |
Correct |
3 ms |
15828 KB |
Output is correct |
46 |
Correct |
0 ms |
15828 KB |
Output is correct |
47 |
Correct |
0 ms |
15828 KB |
Output is correct |
48 |
Correct |
0 ms |
15828 KB |
Output is correct |
49 |
Correct |
0 ms |
15828 KB |
Output is correct |
50 |
Correct |
0 ms |
15828 KB |
Output is correct |
51 |
Correct |
263 ms |
21760 KB |
Output is correct |
52 |
Correct |
203 ms |
28000 KB |
Output is correct |
53 |
Correct |
353 ms |
24436 KB |
Output is correct |
54 |
Correct |
143 ms |
22288 KB |
Output is correct |
55 |
Correct |
273 ms |
22016 KB |
Output is correct |
56 |
Correct |
213 ms |
28000 KB |
Output is correct |
57 |
Correct |
259 ms |
24820 KB |
Output is correct |
58 |
Correct |
179 ms |
22316 KB |
Output is correct |
59 |
Correct |
253 ms |
21824 KB |
Output is correct |
60 |
Correct |
196 ms |
27992 KB |
Output is correct |
61 |
Correct |
246 ms |
24924 KB |
Output is correct |
62 |
Correct |
153 ms |
22912 KB |
Output is correct |
63 |
Correct |
266 ms |
22064 KB |
Output is correct |
64 |
Correct |
216 ms |
27996 KB |
Output is correct |
65 |
Correct |
326 ms |
25084 KB |
Output is correct |
66 |
Correct |
139 ms |
22384 KB |
Output is correct |
67 |
Correct |
209 ms |
22468 KB |
Output is correct |
68 |
Correct |
193 ms |
27996 KB |
Output is correct |
69 |
Correct |
259 ms |
23772 KB |
Output is correct |
70 |
Correct |
163 ms |
22584 KB |
Output is correct |