Submission #776422

#TimeUsernameProblemLanguageResultExecution timeMemory
776422hyakupElection Campaign (JOI15_election_campaign)C++17
100 / 100
328 ms71756 KiB
#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 (stderr)

election_campaign.cpp:70:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   70 | main(){
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...