답안 #229800

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
229800 2020-05-06T14:55:16 Z CaroLinda Islands (IOI08_islands) C++14
90 / 100
805 ms 131076 KB
#include <bits/stdc++.h>

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

#define mkt make_tuple
#define debug printf
#define all(x) x.begin(),x.end()
#define lp(i,a,b) for(int i = a ; i< b ; i++)
#define ss second
#define ff first
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define mk make_pair

const int MAXN = 1e6+10 ;

using namespace std ;

int N ;
int to[MAXN] ;
ll resp , best_diameter ;
ll W[MAXN] , D[MAXN] ;
ll prof[MAXN][2] ;
vector< int > adj[MAXN] ;
bool vis[MAXN] ;

void dfs( int x )
{
    vis[x] = true ;

    for(int child : adj[x] )
    {
        if(vis[child]) continue ;

        dfs(child) ;

        if( prof[child][0] + W[child] >= prof[x][0] )
        {
            prof[x][1] = prof[x][0] ;
            prof[x][0] = prof[child][0] + W[child] ;
        }
        else if( prof[child][0] + W[child] > prof[x][1] )
            prof[x][1] = prof[child][0] + W[child] ;

    }

    best_diameter = max( best_diameter, prof[x][0] + prof[x][1] ) ;

}

void new_cycle(int x)
{
    vector<int> cycle ;
    while( !vis[x] )
    {
        vis[x] = true ;
        cycle.pb(x) ;
        x = to[x] ;
    }

    reverse( all(cycle) ) ;

    for(int i = cycle.size() - 1 ; cycle[i] != x ; i -- )
    {
        vis[ cycle[i] ] = false ;
        cycle.pop_back() ;
    }

    reverse( all(cycle) ) ;


    best_diameter = 0 ;

    ll sum = 0LL , t = 0LL ;

    for(int i = 0 ; i < cycle.size() ; i++ )
    {
        dfs( cycle[i] ) ;
        sum += W[ cycle[i] ] ;
        if( i ) D[ cycle[i] ] = D[ cycle[i-1] ] + W[ cycle[i-1] ] ;
    }

    for(int i = 0 ; i < cycle.size() ; i++ )
    {
        if( i ) best_diameter = max( best_diameter , t + D[ cycle[i] ] + prof[ cycle[i] ][0] ) ;
        t = max(t , -D[ cycle[i] ] + prof[ cycle[i] ][0] ) ;
    }

    t = 0LL ;
    for(int i = 0 ; i < cycle.size() ; i++ )
    {
        if(i) best_diameter = max( best_diameter , sum - D[ cycle[i] ] + prof[ cycle[i] ][0] + t ) ;
        t = max( t , D[ cycle[i] ] + prof[ cycle[i] ][0] ) ;
    }

    resp += best_diameter ;

}

int main()
{
    scanf("%d", &N ) ;
    for(int i = 1 , u ; i <= N ; i++ )
    {
        scanf("%d%d", &to[i] , &W[i] ) ;
        adj[ to[i] ].pb(i) ;
    }

    for(int i = 1 ; i <= N ; i++ )
        if( !vis[i] ) new_cycle(i) ;

    printf("%lld\n" , resp ) ;

}

Compilation message

islands.cpp:4:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
 #pragma GCC optimization ("O3")
 
islands.cpp:5:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
 #pragma GCC optimization ("unroll-loops")
 
islands.cpp: In function 'void new_cycle(int)':
islands.cpp:79:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0 ; i < cycle.size() ; i++ )
                     ~~^~~~~~~~~~~~~~
islands.cpp:86:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0 ; i < cycle.size() ; i++ )
                     ~~^~~~~~~~~~~~~~
islands.cpp:93:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0 ; i < cycle.size() ; i++ )
                     ~~^~~~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:108:38: warning: format '%d' expects argument of type 'int*', but argument 3 has type 'long long int*' [-Wformat=]
         scanf("%d%d", &to[i] , &W[i] ) ;
                                ~~~~~ ^
islands.cpp:106:21: warning: unused variable 'u' [-Wunused-variable]
     for(int i = 1 , u ; i <= N ; i++ )
                     ^
islands.cpp:105:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &N ) ;
     ~~~~~^~~~~~~~~~~
islands.cpp:108:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &to[i] , &W[i] ) ;
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 16 ms 23808 KB Output is correct
3 Correct 17 ms 23936 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 17 ms 23808 KB Output is correct
6 Correct 17 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 17 ms 23808 KB Output is correct
9 Correct 17 ms 23936 KB Output is correct
10 Correct 18 ms 23808 KB Output is correct
11 Correct 17 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 23936 KB Output is correct
2 Correct 18 ms 23936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 23936 KB Output is correct
2 Correct 19 ms 24064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 24704 KB Output is correct
2 Correct 35 ms 25720 KB Output is correct
3 Correct 27 ms 25088 KB Output is correct
4 Correct 22 ms 24456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 26616 KB Output is correct
2 Correct 56 ms 29232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 98 ms 34856 KB Output is correct
2 Correct 97 ms 36828 KB Output is correct
3 Correct 125 ms 38384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 170 ms 45688 KB Output is correct
2 Correct 202 ms 50668 KB Output is correct
3 Correct 231 ms 57476 KB Output is correct
4 Correct 285 ms 60644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 323 ms 40540 KB Output is correct
2 Correct 592 ms 66416 KB Output is correct
3 Correct 325 ms 66680 KB Output is correct
4 Correct 404 ms 78804 KB Output is correct
5 Correct 397 ms 76624 KB Output is correct
6 Correct 805 ms 72440 KB Output is correct
7 Correct 424 ms 80864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 471 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -