Submission #31435

# Submission time Handle Problem Language Result Execution time Memory
31435 2017-08-25T13:59:18 Z chonka Election Campaign (JOI15_election_campaign) C++
100 / 100
519 ms 39240 KB
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std ;

#define MAXN 100007
#define LOG 19

int n , q ;
vector < int > v[ MAXN ] ;
int LCA[ MAXN ][ LOG + 2 ] ;
int lvl[ MAXN ] ;

vector < int > ord ;

int st[ MAXN ] ;
int en[ MAXN ] ;

struct tuhla {
    int x , y ;
    int val ;
};
tuhla f[ MAXN ] ;
vector < tuhla > p[ MAXN ] ;
long long dp[ MAXN ] ;
long long sm[ MAXN ] ;
long long ans = 0 ;


class Tree {
    public :
    long long tr[ 3 * MAXN ] ;
    void init ( int where , int IL , int IR ) {
        tr[ where ] = 0 ;
        if ( IL == IR ) { return ; }
        int mid = ( IL + IR ) / 2 ;
        init ( 2 * where , IL , mid ) ;
        init ( 2 * where + 1 , mid + 1 , IR ) ;
    }
    void update ( int where , int IL , int IR , int pos , long long val ) {
        if ( IR < pos || pos < IL ) { return ; }
        tr[ where ] += val ;
        if ( IL == IR ) { return ; }
        int mid = ( IL + IR ) / 2 ;
        if ( pos <= mid ) {
            update ( 2 * where , IL , mid , pos , val ) ;
        }
        else {
            update ( 2 * where + 1 , mid + 1 , IR , pos , val ) ;
        }
    }
    long long query ( int where , int IL , int IR , int pos ) {
        if ( IR <= pos ) { return tr[ where ] ; }
        if ( pos < IL ) { return 0 ; }
        int mid = ( IL + IR ) / 2 ;
        return ( query ( 2 * where , IL , mid , pos ) + query ( 2 * where + 1 , mid + 1 , IR , pos ) ) ;
    }
};
Tree w ;

void dfs ( int vertex , int prv ) {
    ord.push_back ( vertex ) ;
    st[ vertex ] = ord.size ( ) ;
    int i ;
    int sz = v[ vertex ].size ( ) ;
    if ( prv != -1 ) {
        for ( i = 1 ; i <= LOG ; i ++ ) {
            LCA[ vertex ][ i ] = LCA[ LCA[ vertex ][ i - 1 ] ][ i - 1 ] ;
        }
    }
    for ( i = 0 ; i < sz ; i ++ ) {
        if ( v[ vertex ][ i ] == prv ) { continue ; }
        LCA[ v[ vertex ][ i ] ][ 0 ] = vertex ;
        lvl[ v[ vertex ][ i ] ] = lvl[ vertex ] + 1 ;
        dfs ( v[ vertex ][ i ] , vertex ) ;
    }
    en[ vertex ] = ord.size ( ) ;
    en[ vertex ] ++ ;
}

int get_LCA ( int x , int y ) {
    int i ;
    for ( i = LOG ; i >= 0 ; i -- ) {
        if ( lvl[ x ] - (1<<i) >= lvl[ y ] ) {
            x = LCA[ x ][ i ] ;
        }
        if ( lvl[ y ] - (1<<i) >= lvl[ x ] ) {
            y = LCA[ y ][ i ] ;
        }
    }
    for ( i = LOG ; i >= 0 ; i -- ) {
        if ( LCA[ x ][ i ] != LCA[ y ][ i ] ) {
            x = LCA[ x ][ i ] ;
            y = LCA[ y ][ i ] ;
        }
    }
    if ( x != y ) { x = LCA[ x ][ 0 ] ; }
    return x ;
}

void rec ( int vertex , int prv ) {
    int i ;
    int sz = v[ vertex ].size ( ) ;
    for ( i = 0 ; i < sz ; i ++ ) {
        if ( v[ vertex ][ i ] == prv ) { continue ; }
        rec ( v[ vertex ][ i ] , vertex ) ;
        dp[ vertex ] += dp[ v[ vertex ][ i ] ] ;
    }
    sm[ vertex ] = dp[ vertex ] ;
    sz = p[ vertex ].size ( ) ;
    for ( i = 0 ; i < sz ; i ++ ) {
        int x = p[ vertex ][ i ].x ;
        int y = p[ vertex ][ i ].y ;
        long long add = 0 ;
        if ( lvl[ x ] > lvl[ y ] ) { swap ( x , y ) ; }
        if ( x == vertex ) {
            add += w.query ( 1 , 1 , n , st[ y ] ) ;
        }
        else {
            add += w.query ( 1 , 1 , n , st[ x ] ) ;
            add += w.query ( 1 , 1 , n , st[ y ] ) ;
        }
        add += p[ vertex ][ i ].val + sm[ vertex ] ;
        if ( dp[ vertex ] < add ) { dp[ vertex ] = add ; }
    }
    if ( ans < dp[ vertex ] ) { ans = dp[ vertex ] ; }
    w.update ( 1 , 1 , n , st[ vertex ] , sm[ vertex ] - dp[ vertex ] ) ;
    w.update ( 1 , 1 , n , en[ vertex ] , -(sm[ vertex ] - dp[ vertex ] ) ) ;
}

void input ( ) {
    scanf ( "%d" , &n ) ;
    int i ;
    for ( i = 1 ; i < n ; i ++ ) {
        int x , y ;
        scanf ( "%d%d" , &x , &y ) ;
        v[ x ].push_back ( y ) ;
        v[ y ].push_back ( x ) ;
    }
}


void solve ( ) {
    dfs ( 1 , -1 ) ;
    scanf ( "%d" , &q ) ;
    int i ;
    w.init ( 1 , 1 , n + 1 ) ;
    for ( i = 1 ; i <= q ; i ++ ) {
        scanf ( "%d%d%d" , &f[ i ].x , &f[ i ].y , &f[ i ].val ) ;
        p[ get_LCA ( f[ i ].x , f[ i ].y ) ].push_back ( f[ i ] ) ;
    }
    rec ( 1 , -1 ) ;
    printf ( "%lld\n" , ans ) ;
}


int main ( ) {
    input ( ) ;
    solve ( ) ;
    return 0 ;
}

Compilation message

election_campaign.cpp: In function 'void input()':
election_campaign.cpp:132:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ( "%d" , &n ) ;
                         ^
election_campaign.cpp:136:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf ( "%d%d" , &x , &y ) ;
                                    ^
election_campaign.cpp: In function 'void solve()':
election_campaign.cpp:145:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ( "%d" , &q ) ;
                         ^
election_campaign.cpp:149:66: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf ( "%d%d%d" , &f[ i ].x , &f[ i ].y , &f[ i ].val ) ;
                                                                  ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 21164 KB Output is correct
2 Correct 0 ms 21164 KB Output is correct
3 Correct 0 ms 21164 KB Output is correct
4 Correct 0 ms 21164 KB Output is correct
5 Correct 146 ms 25320 KB Output is correct
6 Correct 73 ms 37256 KB Output is correct
7 Correct 169 ms 32936 KB Output is correct
8 Correct 103 ms 25784 KB Output is correct
9 Correct 126 ms 30456 KB Output is correct
10 Correct 146 ms 25784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 21164 KB Output is correct
2 Correct 0 ms 21164 KB Output is correct
3 Correct 0 ms 21164 KB Output is correct
4 Correct 173 ms 39236 KB Output is correct
5 Correct 153 ms 39232 KB Output is correct
6 Correct 169 ms 39236 KB Output is correct
7 Correct 163 ms 39232 KB Output is correct
8 Correct 226 ms 39236 KB Output is correct
9 Correct 213 ms 39236 KB Output is correct
10 Correct 266 ms 39232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 21164 KB Output is correct
2 Correct 0 ms 21164 KB Output is correct
3 Correct 0 ms 21164 KB Output is correct
4 Correct 173 ms 39236 KB Output is correct
5 Correct 153 ms 39232 KB Output is correct
6 Correct 169 ms 39236 KB Output is correct
7 Correct 163 ms 39232 KB Output is correct
8 Correct 226 ms 39236 KB Output is correct
9 Correct 213 ms 39236 KB Output is correct
10 Correct 266 ms 39232 KB Output is correct
11 Correct 26 ms 21692 KB Output is correct
12 Correct 246 ms 39236 KB Output is correct
13 Correct 286 ms 39232 KB Output is correct
14 Correct 173 ms 39240 KB Output is correct
15 Correct 296 ms 39232 KB Output is correct
16 Correct 253 ms 39232 KB Output is correct
17 Correct 219 ms 39236 KB Output is correct
18 Correct 253 ms 39236 KB Output is correct
19 Correct 146 ms 39232 KB Output is correct
20 Correct 256 ms 39232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 363 ms 26760 KB Output is correct
2 Correct 153 ms 39236 KB Output is correct
3 Correct 273 ms 34400 KB Output is correct
4 Correct 276 ms 27368 KB Output is correct
5 Correct 256 ms 33640 KB Output is correct
6 Correct 173 ms 28160 KB Output is correct
7 Correct 266 ms 33308 KB Output is correct
8 Correct 263 ms 26804 KB Output is correct
9 Correct 206 ms 39232 KB Output is correct
10 Correct 436 ms 31972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 21164 KB Output is correct
2 Correct 0 ms 21164 KB Output is correct
3 Correct 0 ms 21164 KB Output is correct
4 Correct 0 ms 21164 KB Output is correct
5 Correct 146 ms 25320 KB Output is correct
6 Correct 73 ms 37256 KB Output is correct
7 Correct 169 ms 32936 KB Output is correct
8 Correct 103 ms 25784 KB Output is correct
9 Correct 126 ms 30456 KB Output is correct
10 Correct 146 ms 25784 KB Output is correct
11 Correct 3 ms 21296 KB Output is correct
12 Correct 3 ms 21296 KB Output is correct
13 Correct 0 ms 21296 KB Output is correct
14 Correct 3 ms 21296 KB Output is correct
15 Correct 0 ms 21300 KB Output is correct
16 Correct 3 ms 21296 KB Output is correct
17 Correct 3 ms 21296 KB Output is correct
18 Correct 0 ms 21296 KB Output is correct
19 Correct 0 ms 21296 KB Output is correct
20 Correct 0 ms 21296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 21164 KB Output is correct
2 Correct 0 ms 21164 KB Output is correct
3 Correct 0 ms 21164 KB Output is correct
4 Correct 0 ms 21164 KB Output is correct
5 Correct 146 ms 25320 KB Output is correct
6 Correct 73 ms 37256 KB Output is correct
7 Correct 169 ms 32936 KB Output is correct
8 Correct 103 ms 25784 KB Output is correct
9 Correct 126 ms 30456 KB Output is correct
10 Correct 146 ms 25784 KB Output is correct
11 Correct 3 ms 21164 KB Output is correct
12 Correct 0 ms 21164 KB Output is correct
13 Correct 0 ms 21164 KB Output is correct
14 Correct 173 ms 39236 KB Output is correct
15 Correct 153 ms 39232 KB Output is correct
16 Correct 169 ms 39236 KB Output is correct
17 Correct 163 ms 39232 KB Output is correct
18 Correct 226 ms 39236 KB Output is correct
19 Correct 213 ms 39236 KB Output is correct
20 Correct 266 ms 39232 KB Output is correct
21 Correct 26 ms 21692 KB Output is correct
22 Correct 246 ms 39236 KB Output is correct
23 Correct 286 ms 39232 KB Output is correct
24 Correct 173 ms 39240 KB Output is correct
25 Correct 296 ms 39232 KB Output is correct
26 Correct 253 ms 39232 KB Output is correct
27 Correct 219 ms 39236 KB Output is correct
28 Correct 253 ms 39236 KB Output is correct
29 Correct 146 ms 39232 KB Output is correct
30 Correct 256 ms 39232 KB Output is correct
31 Correct 363 ms 26760 KB Output is correct
32 Correct 153 ms 39236 KB Output is correct
33 Correct 273 ms 34400 KB Output is correct
34 Correct 276 ms 27368 KB Output is correct
35 Correct 256 ms 33640 KB Output is correct
36 Correct 173 ms 28160 KB Output is correct
37 Correct 266 ms 33308 KB Output is correct
38 Correct 263 ms 26804 KB Output is correct
39 Correct 206 ms 39232 KB Output is correct
40 Correct 436 ms 31972 KB Output is correct
41 Correct 3 ms 21296 KB Output is correct
42 Correct 3 ms 21296 KB Output is correct
43 Correct 0 ms 21296 KB Output is correct
44 Correct 3 ms 21296 KB Output is correct
45 Correct 0 ms 21300 KB Output is correct
46 Correct 3 ms 21296 KB Output is correct
47 Correct 3 ms 21296 KB Output is correct
48 Correct 0 ms 21296 KB Output is correct
49 Correct 0 ms 21296 KB Output is correct
50 Correct 0 ms 21296 KB Output is correct
51 Correct 353 ms 26736 KB Output is correct
52 Correct 253 ms 39236 KB Output is correct
53 Correct 313 ms 32180 KB Output is correct
54 Correct 193 ms 27236 KB Output is correct
55 Correct 296 ms 26924 KB Output is correct
56 Correct 196 ms 39236 KB Output is correct
57 Correct 389 ms 32796 KB Output is correct
58 Correct 179 ms 27272 KB Output is correct
59 Correct 223 ms 26816 KB Output is correct
60 Correct 169 ms 39232 KB Output is correct
61 Correct 309 ms 33016 KB Output is correct
62 Correct 183 ms 27868 KB Output is correct
63 Correct 236 ms 27020 KB Output is correct
64 Correct 239 ms 39236 KB Output is correct
65 Correct 519 ms 33152 KB Output is correct
66 Correct 303 ms 27472 KB Output is correct
67 Correct 416 ms 27412 KB Output is correct
68 Correct 286 ms 39232 KB Output is correct
69 Correct 243 ms 31016 KB Output is correct
70 Correct 299 ms 27548 KB Output is correct