This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |