#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 1e5 + 10;
const int inf = 1e9 + 10;
vector<int> adj[maxn], query[maxn];
set< pair< int, int > > s[maxn];
int val[maxn], sub[maxn], lazy[maxn], sum[maxn], resp[maxn];
void dfs( int cur, int pai ){
sub[cur] = 1;
int sumfilhos = 0;
for( int& viz : adj[cur] ){
if( viz == pai ) continue;
dfs( viz, cur );
sub[cur] += sub[viz];
sumfilhos += resp[viz];
if( adj[cur][0] == pai || sub[viz] > sub[adj[cur][0]] ) swap( viz, adj[cur][0] );
}
if( sub[cur] == 1 ){
for( int x : query[cur] ) s[cur].insert({ x, 0 });
//cout << cur << " " << resp[cur] << endl;
return;
}
int big = adj[cur][0];
swap( s[cur], s[big] ); swap( lazy[cur], lazy[big] );
//cout << "------- " << cur << endl;
lazy[cur] += sumfilhos - resp[big];
resp[cur] = sumfilhos;
//cout << lazy[cur] << endl;
for( int x : query[cur] ){
auto it = s[cur].lower_bound({ x, -inf });
if( it != s[cur].end() && it->first == x ){
//cout << "- " << x << endl;
//cout << it->second << " " << lazy[cur] << " " << endl;
resp[cur] = max( resp[cur], ( it->second + lazy[cur] ) + val[x] );
s[cur].erase(it);
}
else{
s[cur].insert({ x, -lazy[cur] + sumfilhos });
}
}
for( int viz : adj[cur] ){
if( viz == pai || viz == big ) continue;
for( auto [ x, v ] : s[viz] ){
v += lazy[viz];
auto it = s[cur].lower_bound({ x, -inf });
if( it != s[cur].end() && it->first == x ){
//cout << "- " << x << " " << viz << endl;
//cout << it->second << " " << lazy[cur] << " " << v << endl;
//cout << ( it->second + lazy[cur] ) + val[x] + v << endl;
resp[cur] = max( resp[cur], ( it->second + lazy[cur] ) + val[x] + v - resp[viz] );
s[cur].erase(it);
}
else{
s[cur].insert({ x, -lazy[cur] + v + sumfilhos - resp[viz] });
}
}
}
//cout << cur << " " << resp[cur] << endl;
}
main(){
int n; cin >> n;
for( int i = 1; i < n; i++ ){
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
int q; cin >> q;
for( int i = 1; i <= q; i++ ){
int a, b; cin >> a >> b >> val[i];
query[a].push_back(i);
query[b].push_back(i);
}
dfs( 1, 1 );
cout << resp[1];
}
Compilation message
election_campaign.cpp:70:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
70 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9648 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
64 ms |
15860 KB |
Output is correct |
6 |
Correct |
59 ms |
30796 KB |
Output is correct |
7 |
Correct |
67 ms |
25756 KB |
Output is correct |
8 |
Correct |
62 ms |
14512 KB |
Output is correct |
9 |
Correct |
67 ms |
22620 KB |
Output is correct |
10 |
Correct |
54 ms |
14536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9840 KB |
Output is correct |
4 |
Correct |
197 ms |
37496 KB |
Output is correct |
5 |
Correct |
177 ms |
37488 KB |
Output is correct |
6 |
Correct |
159 ms |
35412 KB |
Output is correct |
7 |
Correct |
207 ms |
37460 KB |
Output is correct |
8 |
Correct |
187 ms |
37656 KB |
Output is correct |
9 |
Correct |
159 ms |
35404 KB |
Output is correct |
10 |
Correct |
176 ms |
37492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9840 KB |
Output is correct |
4 |
Correct |
197 ms |
37496 KB |
Output is correct |
5 |
Correct |
177 ms |
37488 KB |
Output is correct |
6 |
Correct |
159 ms |
35412 KB |
Output is correct |
7 |
Correct |
207 ms |
37460 KB |
Output is correct |
8 |
Correct |
187 ms |
37656 KB |
Output is correct |
9 |
Correct |
159 ms |
35404 KB |
Output is correct |
10 |
Correct |
176 ms |
37492 KB |
Output is correct |
11 |
Correct |
25 ms |
11348 KB |
Output is correct |
12 |
Correct |
178 ms |
37544 KB |
Output is correct |
13 |
Correct |
180 ms |
37484 KB |
Output is correct |
14 |
Correct |
164 ms |
35372 KB |
Output is correct |
15 |
Correct |
181 ms |
37480 KB |
Output is correct |
16 |
Correct |
164 ms |
35536 KB |
Output is correct |
17 |
Correct |
177 ms |
37492 KB |
Output is correct |
18 |
Correct |
178 ms |
37516 KB |
Output is correct |
19 |
Correct |
162 ms |
35424 KB |
Output is correct |
20 |
Correct |
189 ms |
37536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
327 ms |
71076 KB |
Output is correct |
2 |
Correct |
158 ms |
35348 KB |
Output is correct |
3 |
Correct |
199 ms |
36440 KB |
Output is correct |
4 |
Correct |
223 ms |
37836 KB |
Output is correct |
5 |
Correct |
188 ms |
31724 KB |
Output is correct |
6 |
Correct |
223 ms |
36064 KB |
Output is correct |
7 |
Correct |
206 ms |
36392 KB |
Output is correct |
8 |
Correct |
215 ms |
38112 KB |
Output is correct |
9 |
Correct |
159 ms |
35408 KB |
Output is correct |
10 |
Correct |
217 ms |
35716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9648 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
64 ms |
15860 KB |
Output is correct |
6 |
Correct |
59 ms |
30796 KB |
Output is correct |
7 |
Correct |
67 ms |
25756 KB |
Output is correct |
8 |
Correct |
62 ms |
14512 KB |
Output is correct |
9 |
Correct |
67 ms |
22620 KB |
Output is correct |
10 |
Correct |
54 ms |
14536 KB |
Output is correct |
11 |
Correct |
6 ms |
10068 KB |
Output is correct |
12 |
Correct |
5 ms |
9940 KB |
Output is correct |
13 |
Correct |
6 ms |
9940 KB |
Output is correct |
14 |
Correct |
6 ms |
9940 KB |
Output is correct |
15 |
Correct |
6 ms |
10068 KB |
Output is correct |
16 |
Correct |
5 ms |
9812 KB |
Output is correct |
17 |
Correct |
6 ms |
9952 KB |
Output is correct |
18 |
Correct |
7 ms |
9940 KB |
Output is correct |
19 |
Correct |
5 ms |
9940 KB |
Output is correct |
20 |
Correct |
6 ms |
9940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9648 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
64 ms |
15860 KB |
Output is correct |
6 |
Correct |
59 ms |
30796 KB |
Output is correct |
7 |
Correct |
67 ms |
25756 KB |
Output is correct |
8 |
Correct |
62 ms |
14512 KB |
Output is correct |
9 |
Correct |
67 ms |
22620 KB |
Output is correct |
10 |
Correct |
54 ms |
14536 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9840 KB |
Output is correct |
14 |
Correct |
197 ms |
37496 KB |
Output is correct |
15 |
Correct |
177 ms |
37488 KB |
Output is correct |
16 |
Correct |
159 ms |
35412 KB |
Output is correct |
17 |
Correct |
207 ms |
37460 KB |
Output is correct |
18 |
Correct |
187 ms |
37656 KB |
Output is correct |
19 |
Correct |
159 ms |
35404 KB |
Output is correct |
20 |
Correct |
176 ms |
37492 KB |
Output is correct |
21 |
Correct |
25 ms |
11348 KB |
Output is correct |
22 |
Correct |
178 ms |
37544 KB |
Output is correct |
23 |
Correct |
180 ms |
37484 KB |
Output is correct |
24 |
Correct |
164 ms |
35372 KB |
Output is correct |
25 |
Correct |
181 ms |
37480 KB |
Output is correct |
26 |
Correct |
164 ms |
35536 KB |
Output is correct |
27 |
Correct |
177 ms |
37492 KB |
Output is correct |
28 |
Correct |
178 ms |
37516 KB |
Output is correct |
29 |
Correct |
162 ms |
35424 KB |
Output is correct |
30 |
Correct |
189 ms |
37536 KB |
Output is correct |
31 |
Correct |
327 ms |
71076 KB |
Output is correct |
32 |
Correct |
158 ms |
35348 KB |
Output is correct |
33 |
Correct |
199 ms |
36440 KB |
Output is correct |
34 |
Correct |
223 ms |
37836 KB |
Output is correct |
35 |
Correct |
188 ms |
31724 KB |
Output is correct |
36 |
Correct |
223 ms |
36064 KB |
Output is correct |
37 |
Correct |
206 ms |
36392 KB |
Output is correct |
38 |
Correct |
215 ms |
38112 KB |
Output is correct |
39 |
Correct |
159 ms |
35408 KB |
Output is correct |
40 |
Correct |
217 ms |
35716 KB |
Output is correct |
41 |
Correct |
6 ms |
10068 KB |
Output is correct |
42 |
Correct |
5 ms |
9940 KB |
Output is correct |
43 |
Correct |
6 ms |
9940 KB |
Output is correct |
44 |
Correct |
6 ms |
9940 KB |
Output is correct |
45 |
Correct |
6 ms |
10068 KB |
Output is correct |
46 |
Correct |
5 ms |
9812 KB |
Output is correct |
47 |
Correct |
6 ms |
9952 KB |
Output is correct |
48 |
Correct |
7 ms |
9940 KB |
Output is correct |
49 |
Correct |
5 ms |
9940 KB |
Output is correct |
50 |
Correct |
6 ms |
9940 KB |
Output is correct |
51 |
Correct |
247 ms |
37804 KB |
Output is correct |
52 |
Correct |
179 ms |
37444 KB |
Output is correct |
53 |
Correct |
221 ms |
36044 KB |
Output is correct |
54 |
Correct |
185 ms |
30156 KB |
Output is correct |
55 |
Correct |
307 ms |
69920 KB |
Output is correct |
56 |
Correct |
182 ms |
37556 KB |
Output is correct |
57 |
Correct |
185 ms |
31084 KB |
Output is correct |
58 |
Correct |
221 ms |
37960 KB |
Output is correct |
59 |
Correct |
216 ms |
37956 KB |
Output is correct |
60 |
Correct |
181 ms |
37468 KB |
Output is correct |
61 |
Correct |
190 ms |
31180 KB |
Output is correct |
62 |
Correct |
232 ms |
39172 KB |
Output is correct |
63 |
Correct |
328 ms |
70352 KB |
Output is correct |
64 |
Correct |
181 ms |
37512 KB |
Output is correct |
65 |
Correct |
226 ms |
35864 KB |
Output is correct |
66 |
Correct |
189 ms |
28832 KB |
Output is correct |
67 |
Correct |
310 ms |
71756 KB |
Output is correct |
68 |
Correct |
187 ms |
37684 KB |
Output is correct |
69 |
Correct |
189 ms |
29260 KB |
Output is correct |
70 |
Correct |
220 ms |
33956 KB |
Output is correct |