Submission #37239

# Submission time Handle Problem Language Result Execution time Memory
37239 2017-12-22T19:47:47 Z chonka Hard route (IZhO17_road) C++
100 / 100
1463 ms 58352 KB
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std ;

#define MAXN 500007

int n ;
vector < int > v[ MAXN ] ;

struct tuhla {
    int val ;
    int tm ;
    tuhla ( ) { val = tm = 0 ; }
    tuhla ( int _val , int _tm ) {
        val = _val ;
        tm = _tm ;
    }
};
tuhla dp[ MAXN ] ;
tuhla up[ MAXN ] ;

long long ans ;
long long tot ;

vector < int > pos ;

tuhla update ( tuhla cur , int x , int y ) {
    if ( cur.val < x ) { cur.val = x ; cur.tm = 0 ; }
    if ( cur.val == x ) { cur.tm += y ; }
    return cur ;
}

void dfs_down ( int vertex , int prv ) {
    int i ;
    int sz = v[ vertex ].size ( ) ;
    dp[ vertex ] = tuhla ( 0 , 1 ) ;
    for ( i = 0 ; i < sz ; i ++ ) {
        if ( v[ vertex ][ i ] == prv ) { continue ; }
        dfs_down ( v[ vertex ][ i ] , vertex ) ;
        dp[ vertex ] = update ( dp[ vertex ] , dp[ v[ vertex ][ i ] ].val + 1 , dp[ v[ vertex ][ i ] ].tm ) ;
    }
}
void dfs_up ( int vertex , int prv ) {
    int i ;
    int sz = v[ vertex ].size ( ) ;
    tuhla p1 , p2 ;
    int id1 , id2 ;
    id1 = 0 ;
    id2 = 0 ;
    for ( i = 0 ; i < sz ; i ++ ) {
        if ( v[ vertex ][ i ] == prv ) { continue ; }
        up[ v[ vertex ][ i ] ] = tuhla ( 0 , 1 ) ;
        up[ v[ vertex ][ i ] ] = update ( up[ v[ vertex ][ i ] ] , up[ vertex ].val + 1 , up[ vertex ].tm ) ;
    }
    tuhla cur = tuhla ( -50 , 1 ) ;
    for ( i = 0 ; i < sz ; i ++ ) {
        if ( v[ vertex ][ i ] == prv ) { continue ; }
        up[ v[ vertex ][ i ] ] = update ( up[ v[ vertex ][ i ] ] , cur.val , cur.tm ) ;
        cur = update ( cur , dp[ v[ vertex ][ i ] ].val + 2 , dp[ v[ vertex ][ i ] ].tm ) ;
    }
    cur = tuhla ( -50 , 1 ) ;
    for ( i = sz - 1 ; i >= 0 ; i -- ) {
        if ( v[ vertex ][ i ] == prv ) { continue ; }
        up[ v[ vertex ][ i ] ] = update ( up[ v[ vertex ][ i ] ] , cur.val , cur.tm ) ;
        cur = update ( cur , dp[ v[ vertex ][ i ] ].val + 2 , dp[ v[ vertex ][ i ] ].tm ) ;
    }
    for ( i = 0 ; i < sz ; i ++ ) {
        if ( v[ vertex ][ i ] == prv ) { continue ; }
        dfs_up ( v[ vertex ][ i ] , vertex ) ;
    }
}

void recalc ( tuhla &best1 , tuhla &best2 , tuhla &best3 , int x , int y ) {
    if ( best1.val < x ) {
        best3 = best2 ;
        best2 = best1 ;
        best1 = tuhla ( x , y ) ;
    }
    else if ( best2.val < x ) {
        best3 = best2 ;
        best2 = tuhla ( x , y ) ;
    }
    else if ( best3.val < x ) {
        best3 = tuhla ( x , y ) ;
    }
}

void get_mx ( int vertex , int prv ) {
    tuhla best1 , best2 , best3 ;
    best1 = tuhla ( -50 , 1 ) ;
    best2 = tuhla ( -50 , 1 ) ;
    best3 = tuhla ( -50 , 1 ) ;
    int i ;
    int sz = v[ vertex ].size ( ) ;
    if ( prv != -1 ) {
        recalc ( best1 , best2 , best3 , up[ vertex ].val , up[ vertex ].tm ) ;
    }
    for ( i = 0 ; i < sz ; i ++ ) {
        if ( v[ vertex ][ i ] == prv ) { continue ; }
        recalc ( best1 , best2 , best3 , dp[ v[ vertex ][ i ] ].val + 1 , dp[ v[ vertex ][ i ] ].tm ) ;
    }
    if ( best3.val >= 0 ) {
        long long aux = 1LL * best1.val * ( best2.val + best3.val ) ;
        if ( ans < aux ) {
            pos.clear ( ) ;
            ans = aux ;
        }
        if ( ans == aux ) {
            pos.push_back ( vertex ) ;
        }
    }
    for ( i = 0 ; i < sz ; i ++ ) {
        if ( v[ vertex ][ i ] == prv ) { continue ; }
        get_mx ( v[ vertex ][ i ] , vertex ) ;
    }
}

long long calc_fixed_len ( int vertex , int len ) {
    int i ;
    long long ret = 0 ;
    int sz = v[ vertex ].size ( ) ;
    for ( i = 0 ; i < sz ; i ++ ) {
        if ( dp[ v[ vertex ][ i ] ].val + 1 == len ) { ret += dp[ v[ vertex ][ i ] ].tm ; }
    }
    return ret ;
}

long long calc_double ( int vertex , int len ) {
    int i ;
    long long ret = 0 ;
    long long sm = 0 ;
    int sz = v[ vertex ].size ( ) ;
    for ( i = 0 ; i < sz ; i ++ ) {
        if ( dp[ v[ vertex ][ i ] ].val + 1 == len ) {
            ret += sm * dp[ v[ vertex ][ i ] ].tm ;
            sm += dp[ v[ vertex ][ i ] ].tm ;
        }
    }
    return ret ;
}

void get_combinations ( int vertex ) {
    dfs_down ( vertex , -1 ) ;
    tuhla best1 , best2 , best3 ;
    best1 = tuhla ( -50 , 1 ) ;
    best2 = tuhla ( -50 , 1 ) ;
    best3 = tuhla ( -50 , 1 ) ;
    int i ;
    int sz = v[ vertex ].size ( ) ;
    for ( i = 0 ; i < sz ; i ++ ) {
        recalc ( best1 , best2 , best3 , dp[ v[ vertex ][ i ] ].val + 1 , dp[ v[ vertex ][ i ] ].tm ) ;
    }
    if ( best1.val != best2.val && best2.val != best3.val ) {
        long long aux1 , aux2 ;
        aux1 = best2.tm ;
        aux2 = calc_fixed_len ( vertex  , best3.val ) ;
        tot += aux1 * aux2 ;
    }
    else if ( best1.val == best2.val && best2.val != best3.val ) {
        long long aux1 , aux2 ;
        aux1 = best1.tm + best2.tm ;
        aux2 = calc_fixed_len ( vertex , best3.val ) ;
        tot += aux1 * aux2 ;
    }
    else {
        tot += calc_double( vertex , best3.val ) ;
    }
}

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 ( ) {
    int i ;
    int leaves = 0 ;
    for ( i = 1 ; i <= n ; i ++ ) {
        leaves += ( v[ i ].size ( ) == 1 ) ;
    }
    if ( leaves == 2 ) { printf ( "0 1\n" ) ; return ; }
    dfs_down ( 1 , -1 ) ;
    up[ 1 ] = tuhla ( 0 , 1 ) ;
    dfs_up ( 1 , -1 ) ;
    get_mx ( 1 , -1 ) ;
    int sz = pos.size ( ) ;
    for ( i = 0 ; i < sz ; i ++ ) {
        get_combinations ( pos[ i ] ) ;
    }
    printf ( "%lld %lld\n" , ans , tot ) ;
}

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

Compilation message

road.cpp: In function 'void dfs_up(int, int)':
road.cpp:48:9: warning: variable 'id1' set but not used [-Wunused-but-set-variable]
     int id1 , id2 ;
         ^
road.cpp:48:15: warning: variable 'id2' set but not used [-Wunused-but-set-variable]
     int id1 , id2 ;
               ^
road.cpp: In function 'void input()':
road.cpp:172:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ( "%d" , &n ) ;
                         ^
road.cpp:176:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf ( "%d%d" , &x , &y ) ;
                                    ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 21552 KB Output is correct
2 Correct 6 ms 21552 KB Output is correct
3 Correct 0 ms 21552 KB Output is correct
4 Correct 0 ms 21552 KB Output is correct
5 Correct 9 ms 21552 KB Output is correct
6 Correct 3 ms 21552 KB Output is correct
7 Correct 3 ms 21552 KB Output is correct
8 Correct 3 ms 21552 KB Output is correct
9 Correct 3 ms 21552 KB Output is correct
10 Correct 3 ms 21552 KB Output is correct
11 Correct 0 ms 21552 KB Output is correct
12 Correct 0 ms 21552 KB Output is correct
13 Correct 0 ms 21552 KB Output is correct
14 Correct 0 ms 21552 KB Output is correct
15 Correct 9 ms 21552 KB Output is correct
16 Correct 0 ms 21552 KB Output is correct
17 Correct 6 ms 21552 KB Output is correct
18 Correct 3 ms 21552 KB Output is correct
19 Correct 3 ms 21552 KB Output is correct
20 Correct 6 ms 21552 KB Output is correct
21 Correct 6 ms 21552 KB Output is correct
22 Correct 3 ms 21552 KB Output is correct
23 Correct 3 ms 21552 KB Output is correct
24 Correct 3 ms 21552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 21552 KB Output is correct
2 Correct 6 ms 21552 KB Output is correct
3 Correct 0 ms 21552 KB Output is correct
4 Correct 0 ms 21552 KB Output is correct
5 Correct 9 ms 21552 KB Output is correct
6 Correct 3 ms 21552 KB Output is correct
7 Correct 3 ms 21552 KB Output is correct
8 Correct 3 ms 21552 KB Output is correct
9 Correct 3 ms 21552 KB Output is correct
10 Correct 3 ms 21552 KB Output is correct
11 Correct 0 ms 21552 KB Output is correct
12 Correct 0 ms 21552 KB Output is correct
13 Correct 0 ms 21552 KB Output is correct
14 Correct 0 ms 21552 KB Output is correct
15 Correct 9 ms 21552 KB Output is correct
16 Correct 0 ms 21552 KB Output is correct
17 Correct 6 ms 21552 KB Output is correct
18 Correct 3 ms 21552 KB Output is correct
19 Correct 3 ms 21552 KB Output is correct
20 Correct 6 ms 21552 KB Output is correct
21 Correct 6 ms 21552 KB Output is correct
22 Correct 3 ms 21552 KB Output is correct
23 Correct 3 ms 21552 KB Output is correct
24 Correct 3 ms 21552 KB Output is correct
25 Correct 3 ms 21716 KB Output is correct
26 Correct 13 ms 21728 KB Output is correct
27 Correct 3 ms 21756 KB Output is correct
28 Correct 3 ms 21752 KB Output is correct
29 Correct 13 ms 21708 KB Output is correct
30 Correct 6 ms 21728 KB Output is correct
31 Correct 13 ms 21728 KB Output is correct
32 Correct 6 ms 21684 KB Output is correct
33 Correct 3 ms 21720 KB Output is correct
34 Correct 3 ms 21764 KB Output is correct
35 Correct 6 ms 21752 KB Output is correct
36 Correct 9 ms 21732 KB Output is correct
37 Correct 13 ms 21684 KB Output is correct
38 Correct 6 ms 21684 KB Output is correct
39 Correct 6 ms 21684 KB Output is correct
40 Correct 9 ms 21684 KB Output is correct
41 Correct 3 ms 21684 KB Output is correct
42 Correct 13 ms 21684 KB Output is correct
43 Correct 6 ms 21684 KB Output is correct
44 Correct 6 ms 21684 KB Output is correct
45 Correct 6 ms 21684 KB Output is correct
46 Correct 9 ms 21684 KB Output is correct
47 Correct 6 ms 21684 KB Output is correct
48 Correct 6 ms 21816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 21552 KB Output is correct
2 Correct 6 ms 21552 KB Output is correct
3 Correct 0 ms 21552 KB Output is correct
4 Correct 0 ms 21552 KB Output is correct
5 Correct 9 ms 21552 KB Output is correct
6 Correct 3 ms 21552 KB Output is correct
7 Correct 3 ms 21552 KB Output is correct
8 Correct 3 ms 21552 KB Output is correct
9 Correct 3 ms 21552 KB Output is correct
10 Correct 3 ms 21552 KB Output is correct
11 Correct 0 ms 21552 KB Output is correct
12 Correct 0 ms 21552 KB Output is correct
13 Correct 0 ms 21552 KB Output is correct
14 Correct 0 ms 21552 KB Output is correct
15 Correct 9 ms 21552 KB Output is correct
16 Correct 0 ms 21552 KB Output is correct
17 Correct 6 ms 21552 KB Output is correct
18 Correct 3 ms 21552 KB Output is correct
19 Correct 3 ms 21552 KB Output is correct
20 Correct 6 ms 21552 KB Output is correct
21 Correct 6 ms 21552 KB Output is correct
22 Correct 3 ms 21552 KB Output is correct
23 Correct 3 ms 21552 KB Output is correct
24 Correct 3 ms 21552 KB Output is correct
25 Correct 3 ms 21716 KB Output is correct
26 Correct 13 ms 21728 KB Output is correct
27 Correct 3 ms 21756 KB Output is correct
28 Correct 3 ms 21752 KB Output is correct
29 Correct 13 ms 21708 KB Output is correct
30 Correct 6 ms 21728 KB Output is correct
31 Correct 13 ms 21728 KB Output is correct
32 Correct 6 ms 21684 KB Output is correct
33 Correct 3 ms 21720 KB Output is correct
34 Correct 3 ms 21764 KB Output is correct
35 Correct 6 ms 21752 KB Output is correct
36 Correct 9 ms 21732 KB Output is correct
37 Correct 13 ms 21684 KB Output is correct
38 Correct 6 ms 21684 KB Output is correct
39 Correct 6 ms 21684 KB Output is correct
40 Correct 9 ms 21684 KB Output is correct
41 Correct 3 ms 21684 KB Output is correct
42 Correct 13 ms 21684 KB Output is correct
43 Correct 6 ms 21684 KB Output is correct
44 Correct 6 ms 21684 KB Output is correct
45 Correct 6 ms 21684 KB Output is correct
46 Correct 9 ms 21684 KB Output is correct
47 Correct 6 ms 21684 KB Output is correct
48 Correct 6 ms 21816 KB Output is correct
49 Correct 869 ms 53028 KB Output is correct
50 Correct 916 ms 54600 KB Output is correct
51 Correct 966 ms 56928 KB Output is correct
52 Correct 926 ms 53028 KB Output is correct
53 Correct 863 ms 52876 KB Output is correct
54 Correct 946 ms 55424 KB Output is correct
55 Correct 1056 ms 49136 KB Output is correct
56 Correct 936 ms 51864 KB Output is correct
57 Correct 919 ms 58352 KB Output is correct
58 Correct 989 ms 55260 KB Output is correct
59 Correct 936 ms 55212 KB Output is correct
60 Correct 936 ms 53768 KB Output is correct
61 Correct 586 ms 37128 KB Output is correct
62 Correct 576 ms 37128 KB Output is correct
63 Correct 1463 ms 48192 KB Output is correct
64 Correct 1319 ms 43300 KB Output is correct
65 Correct 1339 ms 40148 KB Output is correct
66 Correct 1406 ms 38504 KB Output is correct
67 Correct 1313 ms 37604 KB Output is correct
68 Correct 1313 ms 37248 KB Output is correct
69 Correct 1373 ms 37144 KB Output is correct
70 Correct 1373 ms 37128 KB Output is correct
71 Correct 1366 ms 37128 KB Output is correct
72 Correct 1416 ms 37260 KB Output is correct
73 Correct 1369 ms 37288 KB Output is correct
74 Correct 1319 ms 37264 KB Output is correct
75 Correct 1253 ms 37388 KB Output is correct
76 Correct 1249 ms 37648 KB Output is correct
77 Correct 939 ms 38164 KB Output is correct
78 Correct 526 ms 39212 KB Output is correct