Submission #229806

#TimeUsernameProblemLanguageResultExecution timeMemory
229806CaroLindaIslands (IOI08_islands)C++14
100 / 100
878 ms131072 KiB
#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]; vector<int> cycle ; vector< int > adj[MAXN] ; bool vis[MAXN] ; void dfs( int x ) { vis[x] = true ; ll p2 = 0LL ; for(int child : adj[x] ) { if(vis[child]) continue ; dfs(child) ; if( prof[child] + W[child] >= prof[x] ) { p2 = prof[x] ; prof[x] = prof[child] + W[child] ; } else if( prof[child] + W[child] > p2 ) p2 = prof[child] + W[child] ; } best_diameter = max( best_diameter, prof[x] + p2 ) ; } 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 j=1 ; j <= N ; j++ ) { if(vis[j]) continue ; cycle.clear() ; int x = j ; 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] ] ) ; t = max(t , -D[ cycle[i] ] + prof[ cycle[i] ] ) ; } t = 0LL ; for(int i = 0 ; i < cycle.size() ; i++ ) { if(i) best_diameter = max( best_diameter , sum - D[ cycle[i] ] + prof[ cycle[i] ] + t ) ; t = max( t , D[ cycle[i] ] + prof[ cycle[i] ] ) ; } resp += best_diameter ; } printf("%lld\n" , resp ) ; }

Compilation message (stderr)

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 'int main()':
islands.cpp:62: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:60:21: warning: unused variable 'u' [-Wunused-variable]
     for(int i = 1 , u ; i <= N ; i++ )
                     ^
islands.cpp:95:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0 ; i < cycle.size() ; i++ )
                         ~~^~~~~~~~~~~~~~
islands.cpp:102:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0 ; i < cycle.size() ; i++ )
                         ~~^~~~~~~~~~~~~~
islands.cpp:109:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0 ; i < cycle.size() ; i++ )
                         ~~^~~~~~~~~~~~~~
islands.cpp:59:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &N ) ;
     ~~~~~^~~~~~~~~~~
islands.cpp:62: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] ) ;
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...