#include <bits/stdc++.h>
using namespace std;
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;
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 it2 = s[cur].lower_bound({ x, -inf });
if( it2 != s[cur].end() && it2->first == x ){
//cout << "- " << x << " " << viz << endl;
//cout << it2->second << " " << lazy[cur] << " " << v << endl;
//cout << ( it2->second + lazy[cur] ) + val[x] + v << endl;
resp[cur] = max( resp[cur], ( it2->second + lazy[cur] ) + val[x] + v );
s[cur].erase(it2);
}
else{
s[cur].insert({ x, -lazy[cur] + v + sumfilhos - resp[viz] });
}
}
}
//cout << cur << " " << resp[cur] << endl;
}
int 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];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Incorrect |
4 ms |
9684 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9604 KB |
Output is correct |
3 |
Correct |
5 ms |
9912 KB |
Output is correct |
4 |
Correct |
178 ms |
32552 KB |
Output is correct |
5 |
Correct |
173 ms |
32540 KB |
Output is correct |
6 |
Correct |
157 ms |
30932 KB |
Output is correct |
7 |
Correct |
173 ms |
32576 KB |
Output is correct |
8 |
Correct |
170 ms |
32588 KB |
Output is correct |
9 |
Correct |
152 ms |
31008 KB |
Output is correct |
10 |
Correct |
176 ms |
32500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9604 KB |
Output is correct |
3 |
Correct |
5 ms |
9912 KB |
Output is correct |
4 |
Correct |
178 ms |
32552 KB |
Output is correct |
5 |
Correct |
173 ms |
32540 KB |
Output is correct |
6 |
Correct |
157 ms |
30932 KB |
Output is correct |
7 |
Correct |
173 ms |
32576 KB |
Output is correct |
8 |
Correct |
170 ms |
32588 KB |
Output is correct |
9 |
Correct |
152 ms |
31008 KB |
Output is correct |
10 |
Correct |
176 ms |
32500 KB |
Output is correct |
11 |
Correct |
26 ms |
10860 KB |
Output is correct |
12 |
Correct |
181 ms |
32568 KB |
Output is correct |
13 |
Correct |
180 ms |
32476 KB |
Output is correct |
14 |
Correct |
163 ms |
31036 KB |
Output is correct |
15 |
Correct |
180 ms |
32572 KB |
Output is correct |
16 |
Correct |
160 ms |
31024 KB |
Output is correct |
17 |
Correct |
174 ms |
32592 KB |
Output is correct |
18 |
Correct |
175 ms |
32552 KB |
Output is correct |
19 |
Correct |
175 ms |
30928 KB |
Output is correct |
20 |
Correct |
175 ms |
32472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
320 ms |
54980 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 |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Incorrect |
4 ms |
9684 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 |
4 ms |
9684 KB |
Output is correct |
4 |
Incorrect |
4 ms |
9684 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |