답안 #1042285

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1042285 2024-08-02T19:17:02 Z vjudge1 열대 식물원 (Tropical Garden) (IOI11_garden) C++17
100 / 100
946 ms 32620 KB
#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC target("popcnt")
using namespace std;
 
using ll = long long;
using ull = unsigned long long;
using lld = long double;
using vi = vector<int>;
using vll = vector<ll>;
using ii = pair<int,int>;
using pll = pair<ll, ll>;
using vii = vector<ii>;
using vpll = vector<pll>;
 
#define endl '\n'
#define all(x) x.begin(),x.end()
#define lsb(x) x&(-x)
#define gcd(a,b) __gcd(a,b)
#define sz(x) (int)x.size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fls cout.flush()
 
#define fore(i,l,r) for(auto i=l;i<r;i++)
#define fo(i,n) fore(i,0,n)
#define forex(i,r,l) for(auto i=r; i>=l;i--)
#define ffo(i,n) forex(i,n-1,0)
 
bool cmin(int &a, int b){if(b<a){a=b;return 1;}return 0;}
bool cmax(int &a, int b){if(b>a){a=b;return 1;}return 0;}
void valid(ll in){cout<<((in)?"SI\n":"NO\n");}
ll lcm(ll a, ll b){return (a/gcd(a,b))*b;}
ll gauss(ll n){return (n*(n+1))/2;}
const int N=1e5+5e4+7,INF=(1e9)+3;
struct state{
    int u,t;
    state(){}
    state(int u,int t):u(u),t(t){}
};
int mn[N],mn2[N];
state nxt(int u,int t){
    int v=((t^1)?mn[u]:(mn2[u]==-1?mn[u]:mn2[u]));
    int tv=(mn[v]==u?1:0);
    return {v,tv};
}
state nxt(state e){
    int u=e.u,t=e.t;
    int v=((t^1)?mn[u]:(mn2[u]==-1?mn[u]:mn2[u]));
    int tv=(mn[v]==u?1:0);
    return {v,tv};
}
vii graph1[N];
vector<state> graphinv[N][2];
int md1[N][2],md2[N][2], vis[N][2],timer=0;
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
    fo(i,N)graph1[i].clear(),graphinv[i][0].clear(),graphinv[i][1].clear();
    fo(i,M){
        graph1[R[i][0]].pb({R[i][1], i});
        graph1[R[i][1]].pb({R[i][0], i});
    }
    fo(i,N){
        mn[i]=mn2[i]=-1;
        int m1=M+1,m2=M+1;
        for(auto [v,w]:graph1[i]){
            if(m1>w)m2=m1,m1=w,mn2[i]=mn[i],mn[i]=v;
            else if(m2>w)m2=w,mn2[i]=v;
        }
    }
    fo(i,N){
        fo(j,2){
            state nx=nxt(i,j);
            graphinv[nx.u][nx.t].pb(state(i,j));
            md1[i][j]=md2[i][j]=INF;
        }
    }
    queue<state>q;
    q.push(state(P, 0));
    md1[P][0]=0;
    while(sz(q)){
        state u=q.front();q.pop();
        for(state v:graphinv[u.u][u.t]){
            if(md1[v.u][v.t]==INF){
                md1[v.u][v.t]=md1[u.u][u.t]+1;
                q.push(v);
            }
        }
    }
    q.push(state(P, 1));
    md2[P][1]=0;
    while(sz(q)){
        state u=q.front();q.pop();
        for(state v:graphinv[u.u][u.t]){
            if(md2[v.u][v.t]==INF){
                md2[v.u][v.t]=md2[u.u][u.t]+1;
                q.push(v);
            }
        }
    }
    timer++;
    int toP0=0,toP1=0;
    state act={P,0}; 
    toP0++;
    act=nxt(act);
    while(act.u!=P||act.t!=0){
        vis[act.u][act.t]=timer;
        toP0++;
        act=nxt(act);
        if(vis[act.u][act.t]==timer){
            toP0=INF;
            break;
        }
    }
    act={P,1};
    toP1++;
    act=nxt(act);
    timer++;
    while(act.u!=P||act.t!=1){
        vis[act.u][act.t]=timer;
        toP1++;
        act=nxt(act);
        if(vis[act.u][act.t]==timer){
            toP1=INF;
            break;
        }
    }
    // cout<<toP0<<" "<<toP1<<endl;
    fo(qi,Q){
        int k=G[qi];
        int ans=0;
        fo(i,N){
            if(md1[i][0]<=k&&((k-md1[i][0])%toP0)==0){
                ans++;
                continue;
            }
            if(md2[i][0]<=k&&((k-md2[i][0])%toP1)==0){
                ans++;
                continue;
            }
        }
        answer(ans);
    }
}

Compilation message

garden.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    6 | #pragma GCC optimization ("O3")
      | 
garden.cpp:7: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    7 | #pragma GCC optimization ("unroll-loops")
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 14684 KB Output is correct
6 Correct 2 ms 14940 KB Output is correct
7 Correct 2 ms 14684 KB Output is correct
8 Correct 2 ms 12636 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 14684 KB Output is correct
6 Correct 2 ms 14940 KB Output is correct
7 Correct 2 ms 14684 KB Output is correct
8 Correct 2 ms 12636 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 2 ms 14684 KB Output is correct
11 Correct 7 ms 16632 KB Output is correct
12 Correct 16 ms 17244 KB Output is correct
13 Correct 24 ms 23916 KB Output is correct
14 Correct 41 ms 27612 KB Output is correct
15 Correct 48 ms 28240 KB Output is correct
16 Correct 38 ms 24412 KB Output is correct
17 Correct 37 ms 24400 KB Output is correct
18 Correct 14 ms 17244 KB Output is correct
19 Correct 48 ms 27908 KB Output is correct
20 Correct 50 ms 28220 KB Output is correct
21 Correct 39 ms 24400 KB Output is correct
22 Correct 39 ms 23380 KB Output is correct
23 Correct 44 ms 28756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 14684 KB Output is correct
6 Correct 2 ms 14940 KB Output is correct
7 Correct 2 ms 14684 KB Output is correct
8 Correct 2 ms 12636 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 2 ms 14684 KB Output is correct
11 Correct 7 ms 16632 KB Output is correct
12 Correct 16 ms 17244 KB Output is correct
13 Correct 24 ms 23916 KB Output is correct
14 Correct 41 ms 27612 KB Output is correct
15 Correct 48 ms 28240 KB Output is correct
16 Correct 38 ms 24412 KB Output is correct
17 Correct 37 ms 24400 KB Output is correct
18 Correct 14 ms 17244 KB Output is correct
19 Correct 48 ms 27908 KB Output is correct
20 Correct 50 ms 28220 KB Output is correct
21 Correct 39 ms 24400 KB Output is correct
22 Correct 39 ms 23380 KB Output is correct
23 Correct 44 ms 28756 KB Output is correct
24 Correct 2 ms 14684 KB Output is correct
25 Correct 41 ms 17112 KB Output is correct
26 Correct 65 ms 17444 KB Output is correct
27 Correct 580 ms 24092 KB Output is correct
28 Correct 537 ms 27952 KB Output is correct
29 Correct 641 ms 27984 KB Output is correct
30 Correct 346 ms 25076 KB Output is correct
31 Correct 316 ms 23636 KB Output is correct
32 Correct 86 ms 17464 KB Output is correct
33 Correct 547 ms 27816 KB Output is correct
34 Correct 609 ms 27988 KB Output is correct
35 Correct 357 ms 24580 KB Output is correct
36 Correct 484 ms 24144 KB Output is correct
37 Correct 392 ms 28976 KB Output is correct
38 Correct 946 ms 32620 KB Output is correct