#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 );
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(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9600 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Incorrect |
4 ms |
9776 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9940 KB |
Output is correct |
4 |
Correct |
179 ms |
37472 KB |
Output is correct |
5 |
Correct |
174 ms |
37452 KB |
Output is correct |
6 |
Correct |
168 ms |
35396 KB |
Output is correct |
7 |
Correct |
176 ms |
37544 KB |
Output is correct |
8 |
Correct |
192 ms |
37548 KB |
Output is correct |
9 |
Correct |
163 ms |
35352 KB |
Output is correct |
10 |
Correct |
203 ms |
37452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9940 KB |
Output is correct |
4 |
Correct |
179 ms |
37472 KB |
Output is correct |
5 |
Correct |
174 ms |
37452 KB |
Output is correct |
6 |
Correct |
168 ms |
35396 KB |
Output is correct |
7 |
Correct |
176 ms |
37544 KB |
Output is correct |
8 |
Correct |
192 ms |
37548 KB |
Output is correct |
9 |
Correct |
163 ms |
35352 KB |
Output is correct |
10 |
Correct |
203 ms |
37452 KB |
Output is correct |
11 |
Correct |
25 ms |
11348 KB |
Output is correct |
12 |
Correct |
179 ms |
37428 KB |
Output is correct |
13 |
Correct |
182 ms |
37532 KB |
Output is correct |
14 |
Correct |
166 ms |
35408 KB |
Output is correct |
15 |
Correct |
201 ms |
37484 KB |
Output is correct |
16 |
Correct |
167 ms |
35356 KB |
Output is correct |
17 |
Correct |
192 ms |
37580 KB |
Output is correct |
18 |
Correct |
185 ms |
37488 KB |
Output is correct |
19 |
Correct |
163 ms |
35448 KB |
Output is correct |
20 |
Correct |
182 ms |
37456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
321 ms |
71092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9600 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Incorrect |
4 ms |
9776 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9600 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Incorrect |
4 ms |
9776 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |