# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
229805 | 2020-05-06T15:29:59 Z | CaroLinda | Islands (IOI08_islands) | C++14 | 795 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+2 ; using namespace std ; int N , idx ; int to[MAXN] , cycle[MAXN] ; ll resp , best_diameter , sum , t ; 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) { idx = 0 ; while( !vis[x] ) { vis[x] = true ; cycle[idx++] = x ; x = to[x] ; } reverse(cycle, cycle+idx ) ; while( cycle[idx-1] != x ) { vis[ cycle[idx-1] ] = false ; idx -- ; } reverse( cycle, cycle+idx ) ; best_diameter = sum = t = 0LL ; for(int i = 0 ; i < idx ; 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 < idx ; 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 < idx ; 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
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 23808 KB | Output is correct |
2 | Correct | 18 ms | 23808 KB | Output is correct |
3 | Correct | 18 ms | 23936 KB | Output is correct |
4 | Correct | 21 ms | 23808 KB | Output is correct |
5 | Correct | 18 ms | 23808 KB | Output is correct |
6 | Correct | 18 ms | 23808 KB | Output is correct |
7 | Correct | 18 ms | 23808 KB | Output is correct |
8 | Correct | 18 ms | 23808 KB | Output is correct |
9 | Correct | 18 ms | 23808 KB | Output is correct |
10 | Correct | 19 ms | 23936 KB | Output is correct |
11 | Correct | 18 ms | 23936 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 19 ms | 23936 KB | Output is correct |
2 | Correct | 18 ms | 23936 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 19 ms | 23936 KB | Output is correct |
2 | Correct | 19 ms | 24064 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 25 ms | 24704 KB | Output is correct |
2 | Correct | 34 ms | 25728 KB | Output is correct |
3 | Correct | 28 ms | 25088 KB | Output is correct |
4 | Correct | 23 ms | 24448 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 48 ms | 26488 KB | Output is correct |
2 | Correct | 56 ms | 29180 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 100 ms | 34680 KB | Output is correct |
2 | Correct | 94 ms | 36348 KB | Output is correct |
3 | Correct | 121 ms | 38324 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 171 ms | 45688 KB | Output is correct |
2 | Correct | 203 ms | 50552 KB | Output is correct |
3 | Correct | 209 ms | 56312 KB | Output is correct |
4 | Correct | 275 ms | 60640 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 352 ms | 40752 KB | Output is correct |
2 | Correct | 583 ms | 66400 KB | Output is correct |
3 | Correct | 323 ms | 66552 KB | Output is correct |
4 | Correct | 396 ms | 79096 KB | Output is correct |
5 | Correct | 396 ms | 76792 KB | Output is correct |
6 | Correct | 795 ms | 72312 KB | Output is correct |
7 | Correct | 416 ms | 80760 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 452 ms | 131076 KB | Execution killed with signal 9 (could be triggered by violating memory limits) |
2 | Halted | 0 ms | 0 KB | - |