답안 #875631

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
875631 2023-11-20T09:04:47 Z josanneo22 Islands (IOI08_islands) C++17
100 / 100
119 ms 33624 KB
/*
Problem: P4381 [IOI2008] Island
When:    2023-11-19 19:41:26
Author:  Ning07
*/

#include<bits/stdc++.h>
using namespace std;
using i64 = long long;

namespace fast{
    #pragma GCC optimize(Ofast)
    #pragma GCC optimization (unroll-loops)
    #pragma GCC target(avx,avx2,fma)
    #pragma GCC target(sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native)
    #pragma GCC optimize(3)
    #pragma GCC optimize("inline")
    #pragma GCC optimize("-fgcse")
    #pragma GCC optimize("-fgcse-lm")
    #pragma GCC optimize("-fipa-sra")
    #pragma GCC optimize("-ftree-pre")
    #pragma GCC optimize("-ftree-vrp")
    #pragma GCC optimize("-fpeephole2")
    #pragma GCC optimize("-ffast-math")
    #pragma GCC optimize("-fsched-spec")
    #pragma GCC optimize("unroll-loops")
    #pragma GCC optimize("-falign-jumps")
    #pragma GCC optimize("-falign-loops")
    #pragma GCC optimize("-falign-labels")
    #pragma GCC optimize("-fdevirtualize")
    #pragma GCC optimize("-fcaller-saves")
    #pragma GCC optimize("-fcrossjumping")
    #pragma GCC optimize("-fthread-jumps")
    #pragma GCC optimize("-funroll-loops")
    #pragma GCC optimize("-fwhole-program")
    #pragma GCC optimize("-freorder-blocks")
    #pragma GCC optimize("-fschedule-insns")
    #pragma GCC optimize("inline-functions")
    #pragma GCC optimize("-ftree-tail-merge")
    #pragma GCC optimize("-fschedule-insns2")
    #pragma GCC optimize("-fstrict-aliasing")
    #pragma GCC optimize("-fstrict-overflow")
    #pragma GCC optimize("-falign-functions")
    #pragma GCC optimize("-fcse-skip-blocks")
    #pragma GCC optimize("-fcse-follow-jumps")
    #pragma GCC optimize("-fsched-interblock")
    #pragma GCC optimize("-fpartial-inlining")
    #pragma GCC optimize("no-stack-protector")
    #pragma GCC optimize("-freorder-functions")
    #pragma GCC optimize("-findirect-inlining")
    #pragma GCC optimize("-fhoist-adjacent-loads")
    #pragma GCC optimize("-frerun-cse-after-loop")
    #pragma GCC optimize("inline-small-functions")
    #pragma GCC optimize("-finline-small-functions")
    #pragma GCC optimize("-ftree-switch-conversion")
    #pragma GCC optimize("-foptimize-sibling-calls")
    #pragma GCC optimize("-fexpensive-optimizations")
    #pragma GCC optimize("-funsafe-loop-optimizations")
    #pragma GCC optimize("inline-functions-called-once")
    #pragma GCC optimize("-fdelete-null-pointer-checks")
}
using namespace fast;

inline string read_string(){
    char ch=getchar(); string st1="";
    while (!((ch>='A') && (ch<='Z'))) ch=getchar();
    while ((ch>='A') && (ch<='Z'))  st1+=ch,ch=getchar();
    return st1;
}

template<typename T> inline void re(T &x){ x = 0; T f = 1; char ch = getchar();  while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();} while (isdigit(ch))  {x = x * (1<<1) + x*(1<<3) + (ch-48) ; ch = getchar();} x *= f; }
template<typename x,typename... y>void re(x& a,y&... b){re(a);re(b...);} 
template<typename T> inline void ps(T x) { if(x < 0){ putchar('-'); x = -x; } if(x > 9)  ps(x / 10); putchar(x % 10 + '0');}
template<typename x,typename... y>void ps(x& a,y&... b){ps(a); putchar(' '); ps(b...);} 
#define sp putchar(' ')
#define nl putchar('\n')

const int NN=1e6+500;
int N, A[NN], deg[NN];
i64 ans, f[NN], g[NN], W[NN];
i64 get(int p){
    int st=p; p=A[p];
    i64 m1=f[st],m2=f[st];
    i64 s=W[st],ans1=g[st],ans2=-1E18;
    while (p != st) {
        deg[p] = 0;
        ans1=max(ans1,max(g[p],f[p]+s+m1)); 
        ans2=max(ans2,f[p]-s+m2);
        m1=max(m1,f[p]-s),m2=max(m2,f[p]+s);
        s+=W[p],p=A[p];
    }
    return max(ans1,ans2+s);
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    re(N);
    for(int i=1;i<=N;i++){
        re(A[i],W[i]);
        ++deg[A[i]];
    }
    queue<int> q;
    for(int i=1;i<=N;i++) if(!deg[i]) q.push(i);

    while (!q.empty()){
        int p=q.front(); q.pop();
        i64 c=f[p]+W[p];
        g[A[p]]=max(g[A[p]],max(f[A[p]]+c,g[p]));
        f[A[p]]=max(f[A[p]],c);
        if (!--deg[A[p]]) q.push(A[p]);
    }
    for(int i=1;i<=N;i++) if(deg[i]) ans += get(i);
    ps(ans);
}

Compilation message

islands.cpp:13: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   13 |     #pragma GCC optimization (unroll-loops)
      | 
islands.cpp:12:26: warning: '#pragma GCC optimize' is not a string or number [-Wpragmas]
   12 |     #pragma GCC optimize(Ofast)
      |                          ^~~~~
islands.cpp:14:24: warning: '#pragma GCC option' is not a string [-Wpragmas]
   14 |     #pragma GCC target(avx,avx2,fma)
      |                        ^~~
islands.cpp:15:24: warning: '#pragma GCC option' is not a string [-Wpragmas]
   15 |     #pragma GCC target(sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native)
      |                        ^~~
islands.cpp:35:43: warning: bad option '-fwhole-program' to pragma 'optimize' [-Wpragmas]
   35 |     #pragma GCC optimize("-fwhole-program")
      |                                           ^
islands.cpp:42:45: warning: bad option '-fstrict-overflow' to pragma 'optimize' [-Wpragmas]
   42 |     #pragma GCC optimize("-fstrict-overflow")
      |                                             ^
islands.cpp:44:45: warning: bad option '-fcse-skip-blocks' to pragma 'optimize' [-Wpragmas]
   44 |     #pragma GCC optimize("-fcse-skip-blocks")
      |                                             ^
islands.cpp:58:55: warning: bad option '-funsafe-loop-optimizations' to pragma 'optimize' [-Wpragmas]
   58 |     #pragma GCC optimize("-funsafe-loop-optimizations")
      |                                                       ^
islands.cpp:64:27: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
   64 | inline string read_string(){
      |                           ^
islands.cpp:64:27: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
islands.cpp:64:27: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
islands.cpp:64:27: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
islands.cpp:71:41: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
   71 | template<typename T> inline void re(T &x){ x = 0; T f = 1; char ch = getchar();  while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();} while (isdigit(ch))  {x = x * (1<<1) + x*(1<<3) + (ch-48) ; ch = getchar();} x *= f; }
      |                                         ^
islands.cpp:71:41: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
islands.cpp:71:41: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
islands.cpp:71:41: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
islands.cpp:72:55: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
   72 | template<typename x,typename... y>void re(x& a,y&... b){re(a);re(b...);}
      |                                                       ^
islands.cpp:72:55: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
islands.cpp:72:55: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
islands.cpp:72:55: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
islands.cpp:73:40: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
   73 | template<typename T> inline void ps(T x) { if(x < 0){ putchar('-'); x = -x; } if(x > 9)  ps(x / 10); putchar(x % 10 + '0');}
      |                                        ^
islands.cpp:73:40: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
islands.cpp:73:40: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
islands.cpp:73:40: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
islands.cpp:74:55: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
   74 | template<typename x,typename... y>void ps(x& a,y&... b){ps(a); putchar(' '); ps(b...);}
      |                                                       ^
islands.cpp:74:55: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
islands.cpp:74:55: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
islands.cpp:74:55: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
islands.cpp:81:14: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
   81 | i64 get(int p){
      |              ^
islands.cpp:81:14: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
islands.cpp:81:14: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
islands.cpp:81:14: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
islands.cpp:94:10: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
   94 | int main(){
      |          ^
islands.cpp:94:10: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
islands.cpp:94:10: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
islands.cpp:94:10: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 4 ms 7256 KB Output is correct
3 Correct 2 ms 4952 KB Output is correct
4 Correct 2 ms 4700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7516 KB Output is correct
2 Correct 6 ms 8028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 10128 KB Output is correct
2 Correct 11 ms 8028 KB Output is correct
3 Correct 18 ms 10888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 18324 KB Output is correct
2 Correct 31 ms 21596 KB Output is correct
3 Correct 24 ms 16276 KB Output is correct
4 Correct 46 ms 25940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 19344 KB Output is correct
2 Correct 77 ms 26724 KB Output is correct
3 Correct 55 ms 27728 KB Output is correct
4 Correct 66 ms 33432 KB Output is correct
5 Correct 63 ms 33624 KB Output is correct
6 Correct 113 ms 32592 KB Output is correct
7 Correct 68 ms 33360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 31620 KB Output is correct
2 Correct 64 ms 31704 KB Output is correct
3 Correct 56 ms 17232 KB Output is correct
4 Correct 60 ms 17744 KB Output is correct
5 Correct 64 ms 32916 KB Output is correct
6 Correct 71 ms 33104 KB Output is correct
7 Correct 119 ms 32904 KB Output is correct